-
Notifications
You must be signed in to change notification settings - Fork 4
/
CMakeLists.txt
226 lines (194 loc) · 8.64 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
cmake_minimum_required(VERSION 3.5.0)
set(LIB_NAME utool)
project(${LIB_NAME} C)
set(CMAKE_SH "CMAKE_SH-NOTFOUND")
set(default_build_type "Release")
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to '${default_build_type}' as none was specified.")
set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE
STRING "Choose the type of build." FORCE)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
"Debug" "Release" "MinSizeRel" "RelWithDebInfo")
set(CMAKE_BUILD_TYPE "Release")
endif ()
message("CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}")
#execute_process(COMMAND lsb_release -is
# OUTPUT_VARIABLE RELEASE_CODENAME
# OUTPUT_STRIP_TRAILING_WHITESPACE
# )
set(ALLOW_DUPLICATE_CUSTOM_TARGETS TRUE)
set(CMAKE_C_STANDARD 99)
set(CMAKE_VERBOSE_MAKEFILE ON)
#set(CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS
# OWNER_READ
# OWNER_EXECUTE
# GROUP_READ
# GROUP_EXECUTE)
IF (NOT CMAKE_BUILD_TYPE MATCHES Debug)
if (CMAKE_C_COMPILER_VERSION VERSION_GREATER 4.9)
set(PROTECTOR strong)
else ()
set(PROTECTOR all)
endif ()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -fstack-protector-${PROTECTOR} -Wl,-z,relro -Wl,-z,now ")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,-z,noexecstack -s -D_FORTIFY_SOURCE=2 -O2")
endif ()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DZF_LOG_BUF_SZ=1024 -DZF_LOG_MEM_WIDTH=64")
set(PROJECT_VERSION_MAJOR 1)
set(PROJECT_VERSION_MINOR 40)
set(PROJECT_VERSION_PATCH 1)
set(UTOOL_SO_VERSION 1)
set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
set(MINGW_64_PATH "d:/tools/msys2/mingw64")
#set(MINGW_64_PATH "/d/tools/msys2/mingw64")
set(CYGWIN64_BIN_PATH "/cygdrive/c/cygwin64/bin")
set(UTOOL_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/src/include)
set(UTOOL_BIN_DIR ${PROJECT_SOURCE_DIR}/bin)
set(UTOOL_LIB_DIR ${PROJECT_SOURCE_DIR}/lib)
IF (WIN32)
message("Compile env is window, will use mingw64 to compile utool.")
message("MinGW 64 path is ${MINGW_64_PATH}.")
endif ()
set(CJSON_PATH third-party/cJSON)
set(LIBARGPARSE_PATH third-party/argparse)
set(LIBZFLOG_PATH third-party/zf_log/zf_log)
set(LIBBCF_PATH third-party/bounds_checking_function)
set(LIBCURL_PATH third-party/curl)
IF (WIN32)
set(IPMITOOL_BIN_PATH third-party/ipmi/ipmitool.exe)
else ()
set(IPMITOOL_BIN_PATH third-party/ipmi/ipmitool)
endif ()
## use, i.e. don't skip the full RPATH for the build tree
SET(CMAKE_SKIP_BUILD_RPATH FALSE)
# when building, don't use the install RPATH already
# (but later on when installing)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
# the RPATH to be used when installing
SET(CMAKE_INSTALL_RPATH "\$ORIGIN/libs")
# don't add the automatically determined parts of the RPATH
# which point to directories outside the build tree to the install RPATH
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
#find_package(CURL REQUIRED)
#include_directories(${CURL_INCLUDE_DIR})
# use relatived link path
#cmake_policy(SET CMP0015 OLD)
#cmake_policy(SET CMP0081 OLD)
message(STATUS "Copying libcurl shared libraries.")
set(ThirdPartyLibPath "${CMAKE_CURRENT_BINARY_DIR}/libs")
set(ResourcesPath "${CMAKE_CURRENT_BINARY_DIR}/resources")
file(COPY "${LIBCURL_PATH}/lib/.libs/" DESTINATION "${ThirdPartyLibPath}")
file(COPY "${PROJECT_SOURCE_DIR}/resources/" DESTINATION "${ResourcesPath}")
#configure_file("${LIBCURL_PATH}/lib/.libs/libcurl.so.4.5.0" "${ThirdPartyLibPath}/libcurl.so.4.5.0" COPYONLY)
include_directories(
${UTOOL_INCLUDE_DIR}
${LIBBCF_PATH}/include
${LIBARGPARSE_PATH}
${CJSON_PATH}
${LIBZFLOG_PATH}
${LIBCURL_PATH}/include
)
#link_directories(
# "${LIBCURL_PATH}/lib/.libs"
#)
link_libraries(
curl
)
file(GLOB THIRD_PARTY_SOURCE
${LIBBCF_PATH}/src/*.c
${LIBARGPARSE_PATH}/argparse.c
${LIBZFLOG_PATH}/zf_log.c
${CJSON_PATH}/cJSON.c
${CJSON_PATH}/cJSON_Utils.c)
message(STATUS "third-party source files is ${THIRD_PARTY_SOURCE}")
aux_source_directory(src PRO_SOURCE_FILES)
aux_source_directory(src/commands PRO_SOURCE_FILES)
list(REMOVE_ITEM PRO_SOURCE_FILES "src/main.c")
list(REMOVE_ITEM PRO_SOURCE_FILES "src/main-debug.c")
list(APPEND SOURCE_FILES ${PRO_SOURCE_FILES} ${THIRD_PARTY_SOURCE})
message(STATUS "CMAKE binary dir is ${CMAKE_BINARY_DIR}")
message(STATUS "Source files is ${SOURCE_FILES}")
message(STATUS "Install dir prefix is: ${CMAKE_INSTALL_PREFIX}")
# copy ipmitool
configure_file(${IPMITOOL_BIN_PATH} "${CMAKE_BINARY_DIR}/ipmitool" COPYONLY)
# header files
#aux_source_directory(include INCLUDE_FILES)
install(FILES src/include/utool.h DESTINATION "${CMAKE_INSTALL_PREFIX}/include/")
# shared library
add_library(${LIB_NAME} SHARED ${SOURCE_FILES})
#target_link_libraries(${LIB_NAME} curl)
set_target_properties(${LIB_NAME} PROPERTIES OUTPUT_NAME ${LIB_NAME})
set_target_properties(${LIB_NAME} PROPERTIES LINKER_LANGUAGE C)
set_target_properties("${LIB_NAME}"
PROPERTIES
SOVERSION "${UTOOL_SO_VERSION}"
VERSION "${PROJECT_VERSION}")
set_target_properties(${LIB_NAME} PROPERTIES LINK_FLAGS "-Wl,-rpath,'$ORIGIN/libs' -L./libs")
#install(TARGETS ${LIB_NAME} DESTINATION "${CMAKE_INSTALL_PREFIX}/lib")
install(DIRECTORY
DIRECTORY_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ
DESTINATION "${UTOOL_LIB_DIR}")
install(TARGETS ${LIB_NAME}
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ
DESTINATION "${UTOOL_LIB_DIR}")
install(TARGETS ${LIB_NAME}
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ
DESTINATION "${UTOOL_BIN_DIR}")
# static library
add_library(${LIB_NAME}-static STATIC ${SOURCE_FILES})
set_target_properties(${LIB_NAME}-static PROPERTIES OUTPUT_NAME ${LIB_NAME})
set_target_properties(${LIB_NAME}-static PROPERTIES LINKER_LANGUAGE C)
set_target_properties("${LIB_NAME}-static"
PROPERTIES
SOVERSION "${UTOOL_SO_VERSION}"
VERSION "${PROJECT_VERSION}")
install(TARGETS ${LIB_NAME}-static
PERMISSIONS OWNER_READ GROUP_READ
DESTINATION "${CMAKE_INSTALL_PREFIX}/lib")
install(TARGETS ${LIB_NAME}-static
PERMISSIONS OWNER_READ GROUP_READ
DESTINATION "${UTOOL_LIB_DIR}")
macro(install_symlink filepath sympath)
install(CODE "execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${filepath} ${sympath})")
install(CODE "message(\"-- Created symlink: ${sympath} -> ${filepath}\")")
endmacro(install_symlink)
# build & install executable utool bin
add_executable(${LIB_NAME}-bin src/main.c ${SOURCE_FILES})
set_target_properties(${LIB_NAME}-bin PROPERTIES OUTPUT_NAME ${LIB_NAME})
set_target_properties(${LIB_NAME}-bin PROPERTIES LINK_FLAGS "-Wl,-rpath,'$ORIGIN/libs' -L./libs")
target_compile_options(${LIB_NAME}-bin PRIVATE -fPIE -pie)
install(TARGETS ${LIB_NAME}-bin
PERMISSIONS OWNER_EXECUTE OWNER_READ OWNER_WRITE GROUP_EXECUTE GROUP_READ
DESTINATION ${UTOOL_BIN_DIR})
# debug purpose, should not present in release
IF (CMAKE_BUILD_TYPE MATCHES Debug)
add_executable(${LIB_NAME}-debug src/main-debug.c ${SOURCE_FILES})
set_target_properties(${LIB_NAME}-debug PROPERTIES LINK_FLAGS "-Wl,-rpath,'$ORIGIN/libs' -L./libs")
#install(TARGETS utool-debug DESTINATION ${UTOOL_BIN_DIR})
ENDIF ()
#install(TARGETS utool-bin DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
install(FILES "${IPMITOOL_BIN_PATH}" DESTINATION "${UTOOL_BIN_DIR}")
install(DIRECTORY "${PROJECT_SOURCE_DIR}/resources/" DESTINATION "${UTOOL_BIN_DIR}/resources")
# install libraries
IF (WIN32)
# libcurl
install(FILES "${LIBCURL_PATH}/lib/.libs/libcurl-4.dll" DESTINATION "${UTOOL_BIN_DIR}")
#install(FILES "${LIBCURL_PATH}/lib/.libs/libcurl.dll.a" DESTINATION "${UTOOL_BIN_DIR}")
#install(FILES "${LIBCURL_PATH}/lib/.libs/libcurl.a" DESTINATION "${UTOOL_BIN_DIR}")
#libwinpthread
#install(FILES "${MINGW_64_PATH}/x86_64-w64-mingw32/lib/libwinpthread.a" DESTINATION "${UTOOL_BIN_DIR}")
#install(FILES "${MINGW_64_PATH}/x86_64-w64-mingw32/lib/libwinpthread.dll.a" DESTINATION "${UTOOL_BIN_DIR}")
install(FILES "${MINGW_64_PATH}/bin/libwinpthread-1.dll" DESTINATION "${UTOOL_BIN_DIR}")
# zlib1
install(FILES "${MINGW_64_PATH}/bin/zlib1.dll" DESTINATION "${UTOOL_BIN_DIR}")
else ()
install(DIRECTORY
DIRECTORY_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ
DESTINATION "${UTOOL_BIN_DIR}/libs/")
install(FILES "${LIBCURL_PATH}/lib/.libs/libcurl.so.4.6.0"
PERMISSIONS OWNER_READ GROUP_READ
DESTINATION "${UTOOL_BIN_DIR}/libs/")
install_symlink("./libcurl.so.4.6.0" "../bin/libs/libcurl.so.4")
install_symlink("./libcurl.so.4.6.0" "../bin/libs/libcurl.so")
ENDIF ()