Skip to content

Commit

Permalink
Try windows
Browse files Browse the repository at this point in the history
  • Loading branch information
WardBrian committed Oct 16, 2024
1 parent 23f439e commit 4d95a22
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
22 changes: 21 additions & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
workflow_dispatch: {}

env:
CACHE_VERSION: 0
CACHE_VERSION: 1

# only run one copy per PR
concurrency:
Expand Down Expand Up @@ -37,6 +37,26 @@ jobs:
path: ./stan/
key: ${{ runner.os }}-stan-${{ hashFiles('stan/src/stan/version.hpp') }}-v${{ env.CACHE_VERSION }}


- name: Set up PATH for C example
if: matrix.os == 'windows-latest'
run: |
Add-Content $env:GITHUB_PATH "$(pwd)/stan/lib/stan_math/lib/tbb"
Add-Content $env:GITHUB_PATH "$(pwd)/test_models/full"
- name: Build C example (Windows)
if: matrix.os == 'windows-latest'
run: |
cd c-example/
make -j4 example.exe example_runtime.exe
rm ../src/bridgestan.o
echo "Dynamically linked example"
./example.exe
echo "Runtime loading example"
./example_runtime.exe ../test_models/full/full_model.so
- name: Build C example (Unix)
if: matrix.os != 'windows-latest'
run: |
Expand Down
14 changes: 10 additions & 4 deletions c-example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ include ../Makefile

MODEL?=full
CC = gcc
# .dll on Windows, .so on Linux
ifeq ($(OS_TAG),windows)
DLL = dll
else
DLL = so
endif

../test_models/$(MODEL)/lib$(MODEL)_model.so: ../test_models/$(MODEL)/$(MODEL)_model.so
cp ../test_models/$(MODEL)/$(MODEL)_model.so ../test_models/$(MODEL)/lib$(MODEL)_model.so
../test_models/$(MODEL)/lib$(MODEL)_model.$(DLL): ../test_models/$(MODEL)/$(MODEL)_model.so
cp ../test_models/$(MODEL)/$(MODEL)_model.so ../test_models/$(MODEL)/lib$(MODEL)_model.$(DLL)

example$(EXE): example.c ../test_models/$(MODEL)/lib$(MODEL)_model.so
example$(EXE): example.c ../test_models/$(MODEL)/lib$(MODEL)_model.$(DLL)
$(CC) -c -I ../src example.c -o example.o
$(LINK.c) -o example$(EXE) example.o -Wl,-rpath ../test_models/$(MODEL) -L ../test_models/$(MODEL) -l$(MODEL)_model
$(RM) example.o
Expand All @@ -30,4 +36,4 @@ example_static$(EXE): example.c ../test_models/$(MODEL)/$(MODEL)_model.a
$(RM) example.o

example_runtime$(EXE): runtime_loading.c
$(CC) -I ../src runtime_loading.c -o example_runtime$(EXE) -ldl
$(CC) -I ../src runtime_loading.c -o example_runtime$(EXE)
28 changes: 26 additions & 2 deletions c-example/runtime_loading.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
#include "bridgestan.h"
#include <stdio.h>

#ifdef _WIN32
// hacky way to get dlopen and friends on Windows

#include <libloaderapi.h>
#include <errhandlingapi.h>
#define dlopen(lib, flags) LoadLibraryA(lib)
#define dlsym(handle, sym) GetProcAddress(handle, sym)

char* dlerror() {
DWORD err = GetLastError();
int length = snprintf(NULL, 0, "%d", err);
char* str = malloc(length + 1);
snprintf(str, length + 1, "%d", err);
return str;
}
#else
#include <dlfcn.h>
#endif

#if __STDC_VERSION__ < 202000
#define typeof __typeof__
Expand Down Expand Up @@ -34,8 +52,8 @@ int main(int argc, char** argv) {
int patch = *(int*)dlsym(handle, "bs_patch_version");
fprintf(stderr, "Using BridgeStan version %d.%d.%d\n", major, minor, patch);

// Get function pointers. Uses C23's typeof to re-use bridgestan.h definitions.
// We could also write out the types and not include bridgestan.h
// Get function pointers. Uses C23's typeof to re-use bridgestan.h
// definitions. We could also write out the types and not include bridgestan.h
typeof(&bs_model_construct) bs_model_construct
= dlsym(handle, "bs_model_construct");
typeof(&bs_free_error_msg) bs_free_error_msg
Expand All @@ -45,6 +63,12 @@ int main(int argc, char** argv) {
typeof(&bs_name) bs_name = dlsym(handle, "bs_name");
typeof(&bs_param_num) bs_param_num = dlsym(handle, "bs_param_num");

if (!bs_model_construct || !bs_free_error_msg || !bs_model_destruct ||
!bs_name || !bs_param_num) {
fprintf(stderr, "Error: %s\n", dlerror());
return 1;
}

// from here on, the code is exactly the same as example.c

// this could potentially error, and we may get information back about why.
Expand Down

0 comments on commit 4d95a22

Please sign in to comment.