forked from cainhuang/behaviac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
415 lines (327 loc) · 14.4 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#
# If the user specifies -DCMAKE_BUILD_TYPE on the command line, take their definition
# and dump it in the cache along with proper documentation,
# otherwise set CMAKE_BUILD_TYPE to Release prior to calling PROJECT()
#
if( DEFINED CMAKE_BUILD_TYPE )
set( CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE STRING "Set to either \"Release\" or \"Debug\"" )
else()
set( CMAKE_BUILD_TYPE Release CACHE STRING "Set to either \"Release\" or \"Debug\"" )
endif()
# message( STATUS "CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}" )
mark_as_advanced(CMAKE_CONFIGURATION_TYPES)
project (behaviac)
message( STATUS "MSVC=${MSVC}" )
# if (MSVC)
# cmake_minimum_required (VERSION 3.3)
# else()
# cmake_minimum_required (VERSION 2.8)
# endif()
cmake_minimum_required (VERSION 2.8)
# set(CMAKE_CONFIGURATION_TYPES "Debug;Release;Profile" CACHE STRING "" FORCE)
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "" FORCE)
set (BEHAVIAC_PACKAGE_VERSION 3.5.7)
#option( BUILD_SHARED_LIBS "set to OFF to build static libraries" ON )
SET(BUILD_SHARED_LIBS ON CACHE BOOL "set to OFF to build static libraries")
#option( BUILD_ENABLE_LUA "set to OFF to disable lua" ON )
SET(BUILD_ENABLE_LUA OFF CACHE BOOL "set to OFF to disable lua")
set (BEHAVIAC_ENABLE_LUA 0)
# configure a header file to pass some of the CMake settings
# to the source code
#CONFIGURE_FILE ("${PROJECT_SOURCE_DIR}/Config.h.in" "${PROJECT_BINARY_DIR}/Config.h")
file(GLOB_RECURSE BEHAVIAC_INC "${PROJECT_SOURCE_DIR}/inc/*.*")
file(GLOB_RECURSE BEHAVIAC_SRC "${PROJECT_SOURCE_DIR}/src/*.*")
# exclude_dir = "/exclude_dir/"
macro (ExcludeDir src_files exclude_dir)
foreach (TMP_PATH ${src_files})
string (FIND ${TMP_PATH} ${exclude_dir} EXCLUDE_DIR_FOUND)
if (NOT ${EXCLUDE_DIR_FOUND} EQUAL -1)
list (REMOVE_ITEM ${src_files} ${TMP_PATH})
endif ()
endforeach(TMP_PATH)
endmacro()
if (BUILD_ENABLE_LUA)
set (BEHAVIAC_ENABLE_LUA 1)
#message( STATUS "Enable Lua" )
else()
set (BEHAVIAC_ENABLE_LUA 0)
#message( STATUS "Disable Lua, Excluding Lua ... " )
#ExcludeDir(BEHAVIAC_INC "/lua/")
#ExcludeDir(BEHAVIAC_SRC "/lua/")
endif()
# usage: GroupSources("${PROJECT_SOURCE_DIR}/inc/", "inc")
macro(GroupSources dir prefix)
# file(GLOB_RECURSE elements RELATIVE ${dir} *.h *.hpp *.c *.cpp *.cc)
file(GLOB_RECURSE elements RELATIVE ${dir} *.*)
foreach(element ${elements})
# Extract filename and directory
get_filename_component(element_name ${element} NAME)
get_filename_component(element_dir ${element} DIRECTORY)
if (NOT ${element_dir} STREQUAL "")
# If the file is in a subdirectory use it as source group.
# Use the full hierarchical structure to build source_groups.
string(REPLACE "/" "\\" group_name ${element_dir})
source_group("${prefix}\\${group_name}" FILES ${dir}/${element})
else()
# If the file is in the root directory, place it in the root source_group.
source_group("${prefix}\\" FILES ${dir}/${element})
endif()
endforeach()
endmacro()
if (MSVC)
GroupSources("${PROJECT_SOURCE_DIR}/inc/" "inc")
GroupSources("${PROJECT_SOURCE_DIR}/src/" "src")
endif()
# source_group(inc FILES ${BEHAVIAC_INC})
# source_group(src FILES ${BEHAVIAC_SRC})
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
include_directories ("${PROJECT_SOURCE_DIR}/inc")
if( DEFINED BEHAVIAC_VERSION_MODE )
set(BEHAVIAC_VERSION_MODE ${BEHAVIAC_VERSION_MODE} CACHE STRING "version mode, force to use dev/release version or default(dev version on Debug config and release version on Release config)")
else()
set(BEHAVIAC_VERSION_MODE "Default" CACHE STRING "version mode, force to use dev/release version or default(dev version on Debug config and release version on Release config)")
endif()
SET_PROPERTY(CACHE BEHAVIAC_VERSION_MODE PROPERTY STRINGS Default ForceUseDev ForeUseRelease)
if(NOT(DEFINED BEHAVIAC_VERSION_MODE_LAST))
set(BEHAVIAC_VERSION_MODE_LAST "NotAnOption" CACHE STRING "last version mode")
MARK_AS_ADVANCED (FORCE BEHAVIAC_VERSION_MODE_LAST)
endif()
# set(BEHAVIAC_VERSION_MODE "ForeUseRelease" CACHE STRING "version mode)" FORCE)
if(NOT (${BEHAVIAC_VERSION_MODE} MATCHES ${BEHAVIAC_VERSION_MODE_LAST}))
set(BEHAVIAC_VERSION_MODE_LAST ${BEHAVIAC_VERSION_MODE} CACHE STRING "Updating version mode option" FORCE)
endif()
set (BEHAVIAC_RELEASE_CUSTOMIZED 0)
set (BEHAVIAC_RELEASE_ON 0)
if(${BEHAVIAC_VERSION_MODE} MATCHES "ForceUseDev")
set (BEHAVIAC_RELEASE_CUSTOMIZED 1)
set (BEHAVIAC_RELEASE_ON 0)
endif()
if(${BEHAVIAC_VERSION_MODE} MATCHES "ForeUseRelease")
set (BEHAVIAC_RELEASE_CUSTOMIZED 1)
set (BEHAVIAC_RELEASE_ON 1)
endif()
# set (LINKER_SCRIPT "")
if (CMAKE_GENERATOR MATCHES "Xcode")
message( STATUS "XCODE = ${XCODE}" )
endif()
# if(MSVC OR (CMAKE_GENERATOR MATCHES "Xcode"))
if(MSVC OR XCODE)
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS
$<$<CONFIG:Debug>:_DEBUG>
$<$<CONFIG:Release>:NDEBUG>
)
if (XCODE)
set(XCODE_ATTRIBUTE_GCC_PREPROCESSOR_DEFINITIONS[variant=Debug] "_DEBUG")
set(XCODE_ATTRIBUTE_GCC_PREPROCESSOR_DEFINITIONS[variant=Release] "NDEBUG")
endif()
else()
if( CMAKE_BUILD_TYPE MATCHES Debug )
#add_definitions( "-DDEBUG" )
add_definitions( "-D_DEBUG" )
else()
add_definitions( "-DNDEBUG" )
endif()
endif()
if(BEHAVIAC_RELEASE_ON)
if (MSVC)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Gy /GL")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS} /OPT:ICF /OPT:REF /LTCG")
set(CMAKE_MODULE_LINKER_FLAGS_RELEASE "${CMAKE_MODULE_LINKER_FLAGS} /OPT:ICF /OPT:REF /LTCG")
else()
include( CheckCXXCompilerFlag )
check_cxx_compiler_flag( -flto GXX_HAS_LTO_FLAG )
message( STATUS "GXX_HAS_LTO_FLAG = ${GXX_HAS_LTO_FLAG}" )
if( CMAKE_BUILD_TYPE MATCHES Release AND GXX_HAS_LTO_FLAG )
find_program(CMAKE_GCC_AR NAMES ${_CMAKE_TOOLCHAIN_PREFIX}gcc-ar${_CMAKE_TOOLCHAIN_SUFFIX} HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
find_program(CMAKE_GCC_NM NAMES ${_CMAKE_TOOLCHAIN_PREFIX}gcc-nm HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
find_program(CMAKE_GCC_RANLIB NAMES ${_CMAKE_TOOLCHAIN_PREFIX}gcc-ranlib HINTS ${_CMAKE_TOOLCHAIN_LOCATION})
if( CMAKE_GCC_AR AND CMAKE_GCC_NM AND CMAKE_GCC_RANLIB )
message( STATUS "SET LTO Flags" )
# set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto -fno-fat-lto-objects -O3 -ffunction-sections -fdata-sections -fvisibility=hidden -fvisibility-inlines-hidden" )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto -O3" )
check_cxx_compiler_flag( -fno-fat-lto-objects GXX_HAS_NO_FAT_LTO_FLAG )
message( STATUS "GXX_HAS_NO_FAT_LTO_FLAG = ${GXX_HAS_NO_FAT_LTO_FLAG}" )
if (GXX_HAS_NO_FAT_LTO_FLAG)
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-fat-lto-objects" )
endif()
set( CMAKE_AR "${CMAKE_GCC_AR}" )
set( CMAKE_NM "${CMAKE_GCC_NM}" )
set( CMAKE_RANLIB "${CMAKE_GCC_RANLIB}" )
# set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--gc-sections")
# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
else()
message( WARNING "GCC indicates LTO support, but binutils wrappers could not be found. Disabling LTO." )
endif()
endif()
# -fipa-icf
# -finline-limit=64
# -fno-exceptions -fno-rtti
# -fwhole-program
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffunction-sections -fdata-sections -fvisibility=hidden -O3 -flto")
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Os -flto -fno-fat-lto-objects")
# set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--gc-sections -flto")
# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections -flto")
# set(CMAKE_STATIC_LINKER_FLAGS_RELEASE "${CMAKE_STATIC_LINKER_FLAGS_RELEASE} -flto")
# find_package(Gold)
# if (GOLD_FOUND)
# SET(LINKER_SCRIPT -fuse-ld=gold -Wl,--icf=safe)
# endif()
endif()
endif()
if (BEHAVIAC_ENABLE_LUA)
add_definitions( "-DBEHAVIAC_ENABLE_LUA=1" )
endif()
SET(BUILD_USE_64BITS OFF CACHE BOOL "set to ON to use 64 bits")
if (MSVC)
if (CMAKE_GENERATOR MATCHES "64")
SET(BUILD_USE_64BITS ON CACHE BOOL "CMAKE_GENERATOR is for 64 bit, set to ON accordingly" FORCE)
endif()
else()
# SET(BUILD_USE_64BITS ON CACHE BOOL "CMAKE_GENERATOR is for 64 bit, set to ON accordingly" FORCE)
endif()
if (BUILD_USE_64BITS)
if (MSVC)
if (NOT CMAKE_GENERATOR MATCHES "64")
#
message( SEND_ERROR "You need to specify '${CMAKE_GENERATOR} Win64' to generate project file for 64 bit" )
endif()
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")
# set (CMAKE_OSX_ARCHITECTURES x86_64)
endif()
endif()
message( STATUS "CMAKE_SYSTEM_NAME = ${CMAKE_SYSTEM_NAME}" )
message( STATUS "BUILD_USE_64BITS = ${BUILD_USE_64BITS}" )
message( STATUS "CMAKE_GENERATOR = ${CMAKE_GENERATOR}" )
if (MSVC)
# message( STATUS "MSVC" )
if (BUILD_USE_64BITS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
endif()
endif()
if (VC_MDD_ANDROID)
message( STATUS "VC_MDD_ANDROID" )
cmake_minimum_required (VERSION 3.4)
endif()
set (BEHAVIAC_ANDROID_ON 0)
# demo of the variables being set when VCMDDAndroid is targeted
if(CMAKE_GENERATOR MATCHES "Visual Studio 14" AND CMAKE_SYSTEM_NAME MATCHES "VCMDDAndroid")
# message(STATUS "VCMDDAndroid")
set (BEHAVIAC_ANDROID_ON 1)
endif()
set (COMPILE_POSTFIX "")
if(MSVC OR BEHAVIAC_ANDROID_ON)
message(STATUS "set msvc flags")
# Force to always compile with W4
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
STRING(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif()
#warning treated as error
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX")
#multiple processor compilation
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP /EHs")
set (COMPILE_POSTFIX "msvc")
# hide CMAKE_BUILD_TYPE on msvc
mark_as_advanced(CMAKE_BUILD_TYPE)
if (BEHAVIAC_ANDROID_ON)
message(STATUS "set msvc flags for android")
set (BUILD_SHARED_LIBS OFF CACHE BOOL "set to OFF to build static libraries")
mark_as_advanced(BUILD_SHARED_LIBS)
set (COMPILE_POSTFIX "msvc_android")
endif()
else()
# Update if necessary
set (BUILD_SHARED_LIBS OFF CACHE BOOL "set to OFF to build static libraries" FORCE)
mark_as_advanced(BUILD_SHARED_LIBS)
mark_as_advanced(CMAKE_GNUtoMS)
if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
message(STATUS "set gcc flags")
find_package(Threads REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Woverloaded-virtual -Wnon-virtual-dtor -Wfloat-equal -Wno-unused-local-typedefs -finput-charset=UTF-8")
endif()
# if(CMAKE_GENERATOR STREQUAL Xcode)
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wunused-private-field")
# endif()
set (COMPILE_POSTFIX "gcc")
endif()
message( STATUS "BUILD_SHARED_LIBS = ${BUILD_SHARED_LIBS}" )
message( STATUS "BUILD_ENABLE_LUA = ${BUILD_ENABLE_LUA}" )
message( STATUS "BEHAVIAC_VERSION_MODE = ${BEHAVIAC_VERSION_MODE}" )
message( STATUS "CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}" )
# message( STATUS "BEHAVIAC_VERSION = ${BEHAVIAC_VERSION}" )
if(MSVC OR BEHAVIAC_ANDROID_ON)
message( STATUS "Config = Debug|Release" )
else()
message( STATUS "CMAKE_BUILD_TYPE = ${CMAKE_BUILD_TYPE}" )
endif()
# MESSAGE( STATUS "CMAKE_MODULE_PATH = ${CMAKE_MODULE_PATH}" )
configure_file (
"${PROJECT_SOURCE_DIR}/build/_config.h.in"
"${PROJECT_SOURCE_DIR}/inc/behaviac/base/core/_config.h"
)
set(LIB_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
set(BINARY_PATH ${PROJECT_SOURCE_DIR}/bin)
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${BINARY_PATH} )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIB_OUTPUT_PATH} )
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIB_OUTPUT_PATH} )
set( CMAKE_DEBUG_POSTFIX "_${COMPILE_POSTFIX}_debug" )
set( CMAKE_RELEASE_POSTFIX "_${COMPILE_POSTFIX}_release" )
foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} )
string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${BINARY_PATH} )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${LIB_OUTPUT_PATH} )
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${LIB_OUTPUT_PATH} )
endforeach( OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES )
add_library(libbehaviac ${BEHAVIAC_INC} ${BEHAVIAC_SRC} ${LINKER_SCRIPT})
set_target_properties(libbehaviac PROPERTIES PREFIX "")
# set_target_properties(libbehaviac PROPERTIES OUTPUT_NAME_DEBUG libbehaviac${${CMAKE_DEBUG_POSTFIX}}
# set_target_properties(libbehaviac PROPERTIES OUTPUT_NAME_RELEASE libbehaviac${${CMAKE_RELEASE_POSTFIX}}
set_target_properties(libbehaviac PROPERTIES DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX})
set_target_properties(libbehaviac PROPERTIES RELEASE_POSTFIX ${CMAKE_RELEASE_POSTFIX})
# foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} )
# string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG )
# set_target_properties(libbehaviac PROPERTIES
# ${OUTPUTCONFIG}_OUTPUT_NAME libBehaviac_${OUTPUTCONFIG}
# )
# # ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${LIB_OUTPUT_PATH}
# # LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${LIB_OUTPUT_PATH}
# # RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${BINARY_PATH})
# endforeach( OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES )
# Cmake comes with a builtin add_definitions command to add definitions to
# everything in the current directory, but we need to add definitions to
# targets.
function (add_target_definitions target)
get_target_property(defs ${target} COMPILE_DEFINITIONS)
if (defs MATCHES "NOTFOUND")
set(defs "")
endif ()
foreach (def ${defs} ${ARGN})
list(APPEND deflist ${def})
endforeach ()
set_target_properties(${target} PROPERTIES COMPILE_DEFINITIONS "${deflist}")
endfunction ()
# if (XCODE)
# set_target_properties(libbehaviac PROPERTIES XCODE_ATTRIBUTE_GCC_PREPROCESSOR_DEFINITIONS[variant=Debug] "_DEBUG")
# set_target_properties(libbehaviac PROPERTIES XCODE_ATTRIBUTE_GCC_PREPROCESSOR_DEFINITIONS[variant=Release] "NDEBUG")
# endif()
if( BUILD_SHARED_LIBS )
# SET_TARGET_PROPERTIES(libbehaviac PROPERTIES
# COMPILE_DEFINITIONS "BEHAVIACDLL_EXPORTS")
message( STATUS "build shared libs" )
add_target_definitions(libbehaviac "BEHAVIACDLL_EXPORTS" "BEHAVIAC_DLL")
else()
message( STATUS "build static libs" )
endif()
add_subdirectory ("${PROJECT_SOURCE_DIR}/test/btunittest")
add_subdirectory ("${PROJECT_SOURCE_DIR}/test/btremotetest")
add_subdirectory ("${PROJECT_SOURCE_DIR}/test/demo_running")
add_subdirectory ("${PROJECT_SOURCE_DIR}/test/usertest")
if (MSVC)
add_subdirectory ("${PROJECT_SOURCE_DIR}/test/btperformance")
add_subdirectory ("${PROJECT_SOURCE_DIR}/example/spaceship")
endif()
#add_subdirectory ("${PROJECT_SOURCE_DIR}/example/airbattledemo")