-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild-utils.cmake
134 lines (126 loc) · 5.51 KB
/
build-utils.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#[[
Copyright (c) 2022 nicegraf contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the “Software”), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
# This function adds a new target and sets some configuration options for it.
# Parameters:
# TYPE - type of the target. Must be one of:
# - `lib`, for a static library;
# - `hdr`, for a header-only library;
# - `exe`, for an executable binary.
# SRCS - a list of source files for the target.
# COPTS - a list of compiler options.
# PVT_INCLUDES - a list of paths to add to this target's include paths.
# PUB_INCLUDES - a list of paths to add to the include paths of all targets depending on this target.
# PVT_DEFINES - a list of preprocessor definitions to add for this target.
# PUB_DEFINES - a list of preprocessor definitions to add to all targets depending on this target.
# OUTPUT_DIR - the path to the folder where the output for this target shall be stored.
function (nmk_target)
cmake_parse_arguments(TGT "" "NAME;TYPE" "SRCS;DEPS;COPTS;PUB_INCLUDES;PVT_INCLUDES;PUB_DEFINES;PUB_DEPS;PVT_DEFINES;OUTPUT_DIR;VS_DEBUGGER_WORKING_DIR" ${ARGN})
if (TGT_TYPE STREQUAL "lib")
add_library(${TGT_NAME} STATIC ${TGT_SRCS})
elseif(TGT_TYPE STREQUAL "dll")
add_library(${TGT_NAME} SHARED ${TGT_SRCS})
elseif(TGT_TYPE STREQUAL "hdr")
add_library(${TGT_NAME} INTERFACE ${TGT_SRCS})
elseif(TGT_TYPE STREQUAL "exe")
add_executable(${TGT_NAME} ${TGT_SRCS})
else()
message(FATAL_ERROR "invalid target type ${TGT_NAME}")
endif()
# Add dependencies.
if ( TGT_DEPS )
target_link_libraries(${TGT_NAME} PRIVATE ${TGT_DEPS})
endif()
if ( TGT_PUB_DEPS )
target_link_libraries(${TGT_NAME} INTERFACE ${TGT_PUB_DEPS})
endif()
# Add include directories.
if ( TGT_PUB_INCLUDES )
target_include_directories(${TGT_NAME}
INTERFACE ${TGT_PUB_INCLUDES})
endif()
if ( TGT_PVT_INCLUDES )
target_include_directories(${TGT_NAME}
PRIVATE ${TGT_PVT_INCLUDES})
endif()
if ( NOT ( TGT_TYPE STREQUAL "hdr" ) )
target_include_directories(${TGT_NAME}
PRIVATE ${CMAKE_CURRENT_LIST_DIR}/source ${CMAKE_CURRENT_LIST_DIR}/include)
endif()
target_include_directories(${TGT_NAME}
INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)
# Add compile-time definitions.
if ( TGT_PUB_DEFINES )
target_compile_definitions(${TGT_NAME} INTERFACE ${TGT_PUB_DEFINES})
endif()
if ( TGT_PVT_DEFINES )
target_compile_definitions(${TGT_NAME} PRIVATE ${TGT_PVT_DEFINES})
endif()
# Add compiler options.
if ( NOT ( TGT_TYPE STREQUAL "hdr" ) )
if ( NICEMAKE_COMMON_COMPILE_OPTS )
target_compile_options(${TGT_NAME} PRIVATE ${NICEMAKE_COMMON_COMPILE_OPTS})
endif()
if ( TGT_COPTS )
target_compile_options(${TGT_NAME} PRIVATE ${TGT_COPTS})
endif()
endif()
# Set output directory.
if( TGT_OUTPUT_DIR )
message("Setting output dir for ${TGT_NAME} to ${TGT_OUTPUT_DIR}")
set_target_properties(${TGT_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${TGT_OUTPUT_DIR}")
set_target_properties(${TGT_NAME} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "${TGT_OUTPUT_DIR}")
set_target_properties(${TGT_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY_DEBUG "${TGT_OUTPUT_DIR}")
set_target_properties(${TGT_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY_RELEASE "${TGT_OUTPUT_DIR}")
set_target_properties(${TGT_NAME} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY_DEBUG "${TGT_OUTPUT_DIR}")
set_target_properties(${TGT_NAME} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY_RELEASE "${TGT_OUTPUT_DIR}")
set_target_properties(${TGT_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${TGT_OUTPUT_DIR}")
endif()
/* lifted from stackoverflow */
foreach(source IN LISTS TGT_SRCS)
if (IS_ABSOLUTE "${source}")
file(RELATIVE_PATH _source_rel "${CMAKE_CURRENT_SOURCE_DIR}" "${source}")
else()
set(_source_rel "${source}")
endif()
get_filename_component(_source_path "${_source_rel}" PATH)
string(REPLACE "/" "\\" _source_path_msvc "${_source_path}")
source_group("${_source_path_msvc}" FILES "${source}")
endforeach()
endfunction()
# Shortcut for adding a new library target.
function (nmk_static_library)
nmk_target(TYPE lib ${ARGN})
endfunction()
function (nmk_shared_library)
nmk_target(TYPE dll ${ARGN})
endfunction()
# Shortcut for adding a new header-only library target.
function (nmk_header_library)
nmk_target(TYPE hdr ${ARGN})
endfunction()
# Shortcut for adding a new executable target.
function (nmk_binary)
nmk_target(TYPE exe ${ARGN})
endfunction()