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 c854485
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
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 TBB for C example
if: matrix.os == 'windows-latest'
run: |
Add-Content $env:GITHUB_PATH "$(pwd)/stan/lib/stan_math/lib/tbb"
- name: Build C example (Windows)
if: matrix.os == 'windows-latest'
run: |
cd c-example/
make -j4 example.exe example_static.exe example_runtime.exe
rm ../src/bridgestan.o ../test_models/full/*.a
echo "Dynamically linked example"
./example.exe
echo "Statically linked example"
./example_static.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
2 changes: 1 addition & 1 deletion c-example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,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 c854485

Please sign in to comment.