-
Notifications
You must be signed in to change notification settings - Fork 35
/
pd.cmake
119 lines (106 loc) · 4.99 KB
/
pd.cmake
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
if(APPLE)
set(PD_EXTENSION ".darwin-fat-32.so" CACHE STRING "File extension for pd external")
elseif(UNIX)
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64")
set(PD_EXTENSION ".l_amd64" CACHE STRING "File extension for pd external")
else()
set(PD_EXTENSION ".l_arm64" CACHE STRING "File extension for pd external")
endif()
elseif(WIN32)
set(PD_EXTENSION ".m_amd64" CACHE STRING "File extension for pd external")
endif()
# The path to this file.
set(PD_CMAKE_PATH ${CMAKE_CURRENT_LIST_DIR})
# The path to Pure Data sources.
set(PD_PATH)
set(PD_SOURCES_PATH ${PD_PATH}/src)
# The output path for the externals.
set(PD_OUTPUT_PATH)
if(WIN32)
if(NOT PD_LIB_PATH)
if(PD_PATH)
set(PD_LIB_PATH ${PD_PATH}/bin)
else()
if("${CMAKE_GENERATOR}" MATCHES "(Win64|IA64)" OR "${CMAKE_GENERATOR_PLATFORM}" MATCHES "x64" OR "$ENV{VSCMD_ARG_TGT_ARCH}" MATCHES "x64")
set(PD_LIB_PATH ${PD_CMAKE_PATH}/x64)
else()
set(PD_LIB_PATH ${PD_CMAKE_PATH}/x86)
endif()
endif()
endif()
endif()
# The function adds an external to the project.
# PROJECT_NAME is the name of your project (for example: freeverb_project)
# EXTERNAL_NAME is the name of your external (for example: freeverb~)
# EXTERNAL_SOURCES are the source files (for example: freeverb~.c)
# The function should be call:
# add_external(freeverb_project freeverb~ "userpath/freeverb~.c userpath/otherfile.c")
# later see how to manage relative and absolute path
function(add_pd_external PROJECT_NAME EXTERNAL_NAME EXTERNAL_SOURCES)
source_group(src FILES ${EXTERNAL_SOURCES})
add_library(${PROJECT_NAME} SHARED ${EXTERNAL_SOURCES})
# Includes the path to Pure Data sources.
target_include_directories(${PROJECT_NAME} PRIVATE ${PD_SOURCES_PATH})
# Defines platform specific suffix and the linking necessities.
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "")
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ${PD_EXTENSION})
if(APPLE)
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
elseif(WIN32)
find_library(PD_LIBRARY NAMES pd HINTS ${PD_LIB_PATH})
target_link_libraries(${PROJECT_NAME} PRIVATE ws2_32 ${PD_LIBRARY})
endif()
# Removes some warning for Microsoft Visual C.
if(${MSVC})
set_property(TARGET ${PROJECT_NAME} APPEND_STRING PROPERTY COMPILE_FLAGS "/wd4091 /wd4996")
endif()
# Adds
if(WIN32)
if(${CMAKE_SIZEOF_VOID_P} EQUAL 8)
target_compile_definitions(${PROJECT_NAME} PUBLIC PD_LONGINTTYPE=__int64)
endif()
target_compile_definitions(${PROJECT_NAME} PUBLIC MSW=1 NT=1)
endif()
# Support for PD double precision
if(PD_FLOATSIZE64)
set_property(TARGET ${PROJECT_NAME} APPEND_STRING PROPERTY COMPILE_FLAGS " -DPD_FLOATSIZE=64")
endif()
# Defines the name of the external.
# On XCode with CMake < 3.4 if the name of an external ends with tilde but doesn't have a dot, the name must be 'name~'.
# CMake 3.4 is not sure, but it should be between 3.3.2 and 3.6.2
string(FIND ${EXTERNAL_NAME} "." NAME_HAS_DOT)
string(FIND ${EXTERNAL_NAME} "~" NAME_HAS_TILDE)
if((${CMAKE_VERSION} VERSION_LESS 3.4) AND (CMAKE_GENERATOR STREQUAL Xcode) AND (NAME_HAS_DOT EQUAL -1) AND (NAME_HAS_TILDE GREATER -1))
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME '${EXTERNAL_NAME}')
else()
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME ${EXTERNAL_NAME})
endif()
# Generate the function to export for Windows
if(MSVC)
if(NAME_HAS_DOT EQUAL -1)
string(REPLACE "~" "_tilde" EXPORT_FUNCTION "${EXTERNAL_NAME}_setup")
else()
string(REPLACE "." "0x2e" TEMP_NAME "${EXTERNAL_NAME}")
string(REPLACE "~" "_tilde" EXPORT_FUNCTION "setup_${TEMP_NAME}")
endif()
set_property(TARGET ${PROJECT_NAME} APPEND_STRING PROPERTY LINK_FLAGS "/export:${EXPORT_FUNCTION}")
endif()
# Defines the output path of the external.
set_target_properties(${PROJECT_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PD_OUTPUT_PATH})
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PD_OUTPUT_PATH})
set_target_properties(${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${PD_OUTPUT_PATH})
foreach(OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES})
string(TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG)
set_target_properties(${PROJECT_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${PD_OUTPUT_PATH})
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${PD_OUTPUT_PATH})
set_target_properties(${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${PD_OUTPUT_PATH})
endforeach(OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES)
endfunction(add_pd_external)
# The macro defines the output path of the externals.
macro(set_pd_external_path EXTERNAL_PATH)
set(PD_OUTPUT_PATH ${EXTERNAL_PATH})
endmacro(set_pd_external_path)
# The macro sets the location of Pure Data sources.
macro(set_pd_sources PD_SOURCES)
set(PD_SOURCES_PATH ${PD_SOURCES})
endmacro(set_pd_sources)