From 9fb32528d65a6e90bc73c1b0ecbf475263b7e896 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Sat, 17 Oct 2020 15:18:54 -0400 Subject: [PATCH] Generate *_API compiler definitions for modules... ... So mod-foo defines FOO_API properly on the command line, for use in its public header files; on Windows, appropriately expanding one way when compiling the library, another way when compiling other code that uses it, so that all will link correctly. --- .../cmake-modules/AudacityFunctions.cmake | 43 ++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/cmake-proxies/cmake-modules/AudacityFunctions.cmake b/cmake-proxies/cmake-modules/AudacityFunctions.cmake index 3fea7e2e0..b54e34ae0 100644 --- a/cmake-proxies/cmake-modules/AudacityFunctions.cmake +++ b/cmake-proxies/cmake-modules/AudacityFunctions.cmake @@ -221,6 +221,39 @@ function( audacity_append_common_compiler_options var ) set( ${var} "${${var}}" PARENT_SCOPE ) endfunction() +function( import_export_symbol var module_name ) + # compute, e.g. "TRACK_UI_API" from module name "mod-track-ui" + string( REGEX REPLACE "^mod-" "" symbol "${module_name}" ) + string( TOUPPER "${symbol}" symbol ) + string( REPLACE "-" "_" symbol "${symbol}" ) + string( APPEND symbol "_API" ) + set( "${var}" "${symbol}" PARENT_SCOPE ) +endfunction() + +function( import_symbol_define var module_name ) + import_export_symbol( symbol "${module_name}" ) + if( CMAKE_SYSTEM_NAME MATCHES "Windows" ) + set( value "_declspec(dllimport)" ) + elseif( HAVE_VISIBILITY ) + set( value "__attribute__((visibility(\"default\")))" ) + else() + set( value "" ) + endif() + set( "${var}" "${symbol}=${value}" PARENT_SCOPE ) +endfunction() + +function( export_symbol_define var module_name ) + import_export_symbol( symbol "${module_name}" ) + if( CMAKE_SYSTEM_NAME MATCHES "Windows" ) + set( value "_declspec(dllexport)" ) + elseif( HAVE_VISIBILITY ) + set( value "__attribute__((visibility(\"default\")))" ) + else() + set( value "" ) + endif() + set( "${var}" "${symbol}=${value}" PARENT_SCOPE ) +endfunction() + function( audacity_module_fn NAME SOURCES IMPORT_TARGETS ADDITIONAL_DEFINES ADDITIONAL_LIBRARIES ) @@ -238,6 +271,14 @@ function( audacity_module_fn NAME SOURCES IMPORT_TARGETS endif() add_library( ${TARGET} ${LIBTYPE} ) + export_symbol_define( export_symbol "${TARGET}" ) + import_symbol_define( import_symbol "${TARGET}" ) + set( DEFINES + ${ADDITIONAL_DEFINES} + PRIVATE "${export_symbol}" + INTERFACE "${import_symbol}" + ) + set( LOPTS PRIVATE $<$:-undefined dynamic_lookup> @@ -266,7 +307,7 @@ function( audacity_module_fn NAME SOURCES IMPORT_TARGETS organize_source( "${TARGET_ROOT}" "" "${SOURCES}" ) target_sources( ${TARGET} PRIVATE ${SOURCES} ) - target_compile_definitions( ${TARGET} PRIVATE ${ADDITIONAL_DEFINES} ) + target_compile_definitions( ${TARGET} PRIVATE ${DEFINES} ) target_compile_options( ${TARGET} ${OPTIONS} ) target_include_directories( ${TARGET} PUBLIC ${TARGET_ROOT} )