十万个为什么 [cmake] 按模块编译
CMakeLists.txt
cmake_minimum_required(VERSION 3.5.0)
project (lua_main VERSION 0.0.1)
set(CMAKE_C_FLAGS "-Wall -O2 -std=gnu11 -m32")
set(CMAKE_CXX_FLAGS "-Wall -O2 -std=gnu++11 -m32")
# 相对build目录
set(OUTPUT_DIR ../lua)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_DIR})
include(cmake/lua.dll.cmake)
include(cmake/lua.exe.cmake)
include(cmake/lpeg.dll.cmake)
include(cmake/sproto.dll.cmake)
cmake/lua.dll.cmake
# 相对CMakeLists.txt目录
set(LUA_SRC_DIR ../lua)
add_library(lua SHARED
${LUA_SRC_DIR}/lapi.c
${LUA_SRC_DIR}/lauxlib.c
${LUA_SRC_DIR}/lbaselib.c
${LUA_SRC_DIR}/lcode.c
${LUA_SRC_DIR}/lcorolib.c
${LUA_SRC_DIR}/lctype.c
${LUA_SRC_DIR}/ldblib.c
${LUA_SRC_DIR}/ldebug.c
${LUA_SRC_DIR}/ldo.c
${LUA_SRC_DIR}/ldump.c
${LUA_SRC_DIR}/lfunc.c
${LUA_SRC_DIR}/lgc.c
${LUA_SRC_DIR}/linit.c
${LUA_SRC_DIR}/liolib.c
${LUA_SRC_DIR}/llex.c
${LUA_SRC_DIR}/lmathlib.c
${LUA_SRC_DIR}/lmem.c
${LUA_SRC_DIR}/loadlib.c
${LUA_SRC_DIR}/lobject.c
${LUA_SRC_DIR}/lopcodes.c
${LUA_SRC_DIR}/loslib.c
${LUA_SRC_DIR}/lparser.c
${LUA_SRC_DIR}/lstate.c
${LUA_SRC_DIR}/lstring.c
${LUA_SRC_DIR}/lstrlib.c
${LUA_SRC_DIR}/ltable.c
${LUA_SRC_DIR}/ltablib.c
${LUA_SRC_DIR}/ltests.c
${LUA_SRC_DIR}/ltm.c
${LUA_SRC_DIR}/lundump.c
${LUA_SRC_DIR}/lutf8lib.c
${LUA_SRC_DIR}/lvm.c
${LUA_SRC_DIR}/lzio.c
)
set_target_properties(lua
PROPERTIES PREFIX ""
)
add_custom_command(TARGET lua POST_BUILD
COMMAND strip ${OUTPUT_DIR}/lua.dll
)
cmake/lua.exe.cmake
add_executable(lua_exe
${LUA_SRC_DIR}/lua.c
)
target_link_libraries(lua_exe
lua
)
set_target_properties(lua_exe
PROPERTIES OUTPUT_NAME "lua"
)
add_custom_command(TARGET lua_exe POST_BUILD
COMMAND strip ${OUTPUT_DIR}/lua.exe
)
cmake/lpeg.dll.cmake
# 相对CMakeLists.txt目录
set(LPEG_SRC_DIR ../skynet/3rd/lpeg)
add_library(lpeg SHARED
${LPEG_SRC_DIR}/lpcap.c
${LPEG_SRC_DIR}/lpcode.c
${LPEG_SRC_DIR}/lpcset.c
${LPEG_SRC_DIR}/lpprint.c
${LPEG_SRC_DIR}/lptree.c
${LPEG_SRC_DIR}/lpvm.c
)
target_include_directories(lpeg PRIVATE
${LUA_SRC_DIR}
)
set_target_properties(lpeg
PROPERTIES PREFIX ""
)
target_link_libraries(lpeg lua)
add_custom_command(TARGET lpeg POST_BUILD
COMMAND strip ${OUTPUT_DIR}/lpeg.dll
COMMAND del lpeg.dll.a liblpeg.dll.a
)
cmake/sproto.dll.cmake
add_library(sproto SHARED
lsproto.c
sproto.c
)
target_include_directories(sproto
PRIVATE ${LUA_SRC_DIR}
)
set_target_properties(sproto
PROPERTIES PREFIX ""
)
target_link_libraries(sproto
lua
)
add_custom_command(TARGET sproto POST_BUILD
COMMAND strip ${OUTPUT_DIR}/sproto.dll
COMMAND del sproto.dll.a libsproto.dll.a
)