-
Notifications
You must be signed in to change notification settings - Fork 8
/
CMakeLists.txt
93 lines (74 loc) · 2.91 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
cmake_minimum_required (VERSION 3.14)
project (ExternalLibrary)
set(EXTERNAL_LANGUAGE "CPP" CACHE STRING "Language of the external functions")
set_property(CACHE EXTERNAL_LANGUAGE PROPERTY STRINGS CPP Python)
set(PYTHON_HOME "E:/Anaconda3/envs/py37_64" CACHE STRING "Python home")
if (MSVC)
# link statically against the the Visual C runtime
string(REPLACE "/MD" "/MT" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
string(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
string(REPLACE "/MDd" "/MTd" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
string(REPLACE "/MDd" "/MTd" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
# disable CRT deprecation warnings
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
endif ()
if (WIN32)
set(MODELICA_PLATFORM win)
elseif (APPLE)
set(MODELICA_PLATFORM darwin)
else ()
set(MODELICA_PLATFORM linux)
endif ()
if ("${CMAKE_SIZEOF_VOID_P}" STREQUAL "8")
set (MODELICA_PLATFORM ${MODELICA_PLATFORM}64)
else ()
set (MODELICA_PLATFORM ${MODELICA_PLATFORM}32)
endif ()
set(SOURCE_FILES
ExternalLibrary/Resources/Include/ModelicaUtilityFunctions.h
C/include/ExternalLibrary.h
)
if ("${EXTERNAL_LANGUAGE}" STREQUAL "CPP")
set (SOURCE_FILES ${SOURCE_FILES} C/src/ExternalLibraryCPP.cpp)
else ()
set (SOURCE_FILES ${SOURCE_FILES} C/src/ExternalLibraryPython.c)
endif ()
add_library(ExternalLibrary SHARED ${SOURCE_FILES})
target_include_directories(ExternalLibrary PUBLIC
ExternalLibrary/Resources/Include
C/include
)
if ("${EXTERNAL_LANGUAGE}" STREQUAL "Python")
if (WIN32)
target_include_directories(ExternalLibrary PUBLIC ${PYTHON_HOME}/include)
file(GLOB PYTHON_LIB ${PYTHON_HOME}/libs/python3?*.lib)
target_link_libraries(ExternalLibrary ${PYTHON_LIB})
file(GLOB PYTHON_DLL ${PYTHON_HOME}/python3?*.dll)
get_filename_component(PYTHON_DLL ${PYTHON_DLL} NAME)
set(CMAKE_SHARED_LINKER_FLAGS " /DELAYLOAD:${PYTHON_LIBRARY_NAME}.dll")
else ()
target_include_directories(ExternalLibrary PUBLIC /usr/include/python3.10)
endif ()
endif ()
SET_TARGET_PROPERTIES(ExternalLibrary PROPERTIES PREFIX "")
add_custom_command(TARGET ExternalLibrary POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
"$<TARGET_FILE:ExternalLibrary>"
"${CMAKE_CURRENT_SOURCE_DIR}/ExternalLibrary/Resources/Library/${MODELICA_PLATFORM}/${CMAKE_SHARED_LIBRARY_PREFIX}ExternalLibrary${CMAKE_SHARED_LIBRARY_SUFFIX}"
)
add_executable(ExternalLibraryTest
C/tests/catch_amalgamated.hpp
C/tests/catch_amalgamated.cpp
C/tests/ExternalLibrary_test.cpp
)
target_compile_definitions(ExternalLibraryTest PUBLIC
DATA_FILE=\"${CMAKE_CURRENT_SOURCE_DIR}/ExternalLibrary/Resources/Data/data.txt\"
PYTHON_HOME=\"${PYTHON_HOME}\"
)
target_include_directories(ExternalLibraryTest PUBLIC
ExternalLibrary/Resources/Include
C/include
)
if (UNIX)
target_link_libraries(ExternalLibraryTest dl)
endif ()
add_dependencies(ExternalLibraryTest ExternalLibrary)