-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.cmake
155 lines (129 loc) · 5.27 KB
/
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
macro(copy_dependency deps target)
if( MSVC )
if("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
get_target_property( DEP_SHARED_LIB_PATH ${deps} IMPORTED_LOCATION_RELEASE )
else()
get_target_property( DEP_SHARED_LIB_PATH ${deps} IMPORTED_LOCATION_DEBUG )
endif()
# Copy the shared lib file
add_custom_command(TARGET ${target} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${DEP_SHARED_LIB_PATH} $<TARGET_FILE_DIR:${target}>)
endif()
endmacro()
function(get_linux_lsb_release_information)
find_program(LSB_RELEASE_EXEC lsb_release)
if(NOT LSB_RELEASE_EXEC)
message(FATAL_ERROR "Could not detect lsb_release executable, can not gather required information")
endif()
execute_process(COMMAND "${LSB_RELEASE_EXEC}" --short --id OUTPUT_VARIABLE LSB_RELEASE_ID_SHORT OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND "${LSB_RELEASE_EXEC}" --short --release OUTPUT_VARIABLE LSB_RELEASE_VERSION_SHORT OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(COMMAND "${LSB_RELEASE_EXEC}" --short --codename OUTPUT_VARIABLE LSB_RELEASE_CODENAME_SHORT OUTPUT_STRIP_TRAILING_WHITESPACE)
set(LSB_RELEASE_ID_SHORT "${LSB_RELEASE_ID_SHORT}" PARENT_SCOPE)
set(LSB_RELEASE_VERSION_SHORT "${LSB_RELEASE_VERSION_SHORT}" PARENT_SCOPE)
set(LSB_RELEASE_CODENAME_SHORT "${LSB_RELEASE_CODENAME_SHORT}" PARENT_SCOPE)
endfunction()
function(find_python_module module)
set(Python3_FIND_REGISTRY "LAST")
find_package(Python3 COMPONENTS Interpreter)
if(Python3_Interpreter_FOUND)
execute_process(
COMMAND ${Python3_EXECUTABLE} -c "import ${module}"
RESULT_VARIABLE EXIT_CODE
OUTPUT_QUIET
ERROR_QUIET
)
if (${EXIT_CODE} EQUAL 0)
set(PYTHON_MODULE_${module}_FOUND "true" PARENT_SCOPE)
endif()
endif()
endfunction()
# build_git_dependency()
#
# CMake function to download, build and install (in staging area) a dependency at configure
# time.
#
# Parameters:
# NAME: name of the dependency
# REPOSITORY: git url of the dependency
# TAG: tag of the dependency
# APPLY_PATCH: apply patch
# CMAKE_ARGS: List of specific CMake args to add
# CONFIGURE_COMMAND : Command used for configure (default empty and default CMake configure use)
# BUILD_COMMAND : Command used for build (default empty and default CMake build use)
# INSTALL_COMMAND : Command used for install (default empty and default CMake instal use)
# INSOURCE_BUILD : If option ON , compilation is done in source directory
# SOURCE_SUBDIR : Indicate where to find CMakeList.txt
#
# build_dependency(
# NAME
# abseil-cpp
# URL
# https://github.com/abseil/abseil-cpp.git
# TAG
# master
# APPLY_PATCH
# ${CMAKE_SOURCE_DIR}/patches/abseil-cpp.patch
# )
function(build_git_dependency)
set(options "")
set(oneValueArgs NAME REPOSITORY TAG APPLY_PATCH CONFIGURE_COMMAND BUILD_COMMAND INSTALL_COMMAND INSOURCE_BUILD SOURCE_SUBDIR)
set(multiValueArgs CMAKE_ARGS)
cmake_parse_arguments(GIT_DEP
"${options}"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
message(STATUS "Building ${GIT_DEP_NAME}: ...")
if(GIT_DEP_APPLY_PATCH)
set(PATCH_CMD "git apply \"${GIT_DEP_APPLY_PATCH}\"")
else()
set(PATCH_CMD "\"\"")
endif()
if(GIT_DEP_CONFIGURE_COMMAND)
set(CONFIGURE_COMMAND "CONFIGURE_COMMAND ${GIT_DEP_CONFIGURE_COMMAND}")
else()
set(CONFIGURE_COMMAND "#CONFIGURE_COMMAND")
endif()
if(GIT_DEP_BUILD_COMMAND)
set(BUILD_COMMAND "BUILD_COMMAND ${GIT_DEP_BUILD_COMMAND}")
else()
set(BUILD_COMMAND "#BUILD_COMMAND")
endif()
if(GIT_DEP_INSTALL_COMMAND)
set(INSTALL_COMMAND "INSTALL_COMMAND ${GIT_DEP_INSTALL_COMMAND}")
else()
set(INSTALL_COMMAND "#INSTALL_COMMAND")
endif()
if(GIT_DEP_SOURCE_SUBDIR)
set(SOURCE_SUBDIR "SOURCE_SUBDIR ${GIT_DEP_SOURCE_SUBDIR}")
else()
set(SOURCE_SUBDIR "#SOURCE_SUBDIR")
endif()
if (GIT_DEP_INSOURCE_BUILD)
set (BINARY_DIR "BUILD_IN_SOURCE 1")
else()
set (BINARY_DIR "BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/${GIT_DEP_NAME}/build")
endif()
#use specific list separator to be able to use a list of CMAKE_PREFIX_PATH
string(REPLACE ";" "|" STR_CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH}")
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt.in
${CMAKE_CURRENT_BINARY_DIR}/${GIT_DEP_NAME}/CMakeLists.txt @ONLY)
execute_process(
COMMAND ${CMAKE_COMMAND} -H. -Bproject_build -G "${CMAKE_GENERATOR}"
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${GIT_DEP_NAME})
if(result)
message(FATAL_ERROR "CMake step for ${GIT_DEP_NAME} failed: ${result}")
endif()
include(ProcessorCount)
ProcessorCount(NB_PROC)
execute_process(
COMMAND ${CMAKE_COMMAND} --build project_build --config ${CMAKE_BUILD_TYPE} -j ${NB_PROC}
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${GIT_DEP_NAME})
if(result)
message(FATAL_ERROR "Build step for ${GIT_DEP_NAME} failed: ${result}")
endif()
message(STATUS "Building ${GIT_DEP_NAME}: ...DONE")
endfunction()