Skip to content

Commit

Permalink
Make MSVC symbol export/import work
Browse files Browse the repository at this point in the history
  • Loading branch information
rgommers committed Oct 27, 2024
1 parent ecc994f commit e53b085
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
4 changes: 3 additions & 1 deletion tests/packages/sharedlib-in-package/mypkg/examplelib.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
//
// SPDX-License-Identifier: MIT

int sum(int a, int b);
#include "mypkg_dll.h"

MYPKG_DLL int sum(int a, int b);
16 changes: 15 additions & 1 deletion tests/packages/sharedlib-in-package/mypkg/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,31 @@
#
# SPDX-License-Identifier: MIT

if meson.get_compiler('c').get_id() in ['msvc', 'clang-cl', 'intel-cl']
export_dll_args = ['-DMYPKG_DLL_EXPORTS']
import_dll_args = ['-DMYPKG_DLL_IMPORTS']
else
export_dll_args = []
import_dll_args = []
endif

example_lib = shared_library(
'examplelib',
'examplelib.c',
c_args: export_dll_args,
install: true,
install_dir: py.get_install_dir() / 'mypkg',
)

example_lib_dep = declare_dependency(
compile_args: import_dll_args,
link_with: example_lib,
)

py.extension_module(
'_example',
'_examplemod.c',
link_with: example_lib,
dependencies: example_lib,
install: true,
subdir: 'mypkg',
install_rpath: '$ORIGIN',
Expand Down
19 changes: 19 additions & 0 deletions tests/packages/sharedlib-in-package/mypkg/mypkg_dll.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

// MYPKG_DLL
// inspired by https://github.com/abseil/abseil-cpp/blob/20240116.2/absl/base/config.h#L736-L753
// and https://github.com/scipy/scipy/blob/9ded83b51099eee745418ccbb30196db96c81f3f/scipy/_build_utils/src/scipy_dll.h
//
// When building the `examplelib` DLL, this macro expands to `__declspec(dllexport)`
// so we can annotate symbols appropriately as being exported. When used in
// headers consuming a DLL, this macro expands to `__declspec(dllimport)` so
// that consumers know the symbol is defined inside the DLL. In all other cases,
// the macro expands to nothing.
// Note: MYPKG_DLL_{EX,IM}PORTS are set in mypkg/meson.build
#if defined(MYPKG_DLL_EXPORTS)
#define MYPKG_DLL __declspec(dllexport)
#elif defined(MYPKG_DLL_IMPORTS)
#define MYPKG_DLL __declspec(dllimport)
#else
#define MYPKG_DLL
#endif

0 comments on commit e53b085

Please sign in to comment.