Skip to content

Instantly share code, notes, and snippets.

@awstanley
Last active August 11, 2019 17:23
Show Gist options
  • Save awstanley/45dab4614c58cd2304b8a5a342f51919 to your computer and use it in GitHub Desktop.
Save awstanley/45dab4614c58cd2304b8a5a342f51919 to your computer and use it in GitHub Desktop.
CMake helper file. I've kept many copies of this semi-private for years, this one just contains a little more code to help people along. (Updated to include some missing defines, all of the ones in the current test project, so as to be sure they're all there.)
# Copyright 2014-2017 A.W. 'aws' Stanley.
#
# Licensed under the Apache Licence, Version 2.0 [1] or
# MIT licence [2], at your option. This file may not be
# copied, modified, or distributed except according to
# those terms.
#
# [1] <http://www.apache.org/licenses/LICENSE-2.0>
# [2] <http://opensource.org/licenses/MIT>
## Unwinds one list into a source group.
# This unwinds a list into source_group and stores files in ${outvar}.
# e.g. aws_unwind(LUA_SOURCE_GROUPS ${EXE_FILES})
macro(aws_unwind list outvar)
set(SGLEN 0)
set(SGPOS 0)
list(LENGTH ${list} SGLEN)
if(NOT ${SGLEN} EQUAL ${SGPOS})
while(TRUE)
set(SGNAME "")
set(SGLISTNAME "")
set(SGLIST "")
list(GET ${list} ${SGPOS} SGNAME)
math(EXPR SGPOS "${SGPOS}+1")
if(${SGPOS} EQUAL ${SGLEN})
break()
endif()
list(GET ${list} ${SGPOS} SGLISTNAME)
math(EXPR SGPOS "${SGPOS}+1")
foreach(a ${${SGLISTNAME}})
list(APPEND SGLIST ${a})
list(APPEND ${outvar} ${a})
endforeach()
source_group(${SGNAME} FILES ${SGLIST})
if(${SGPOS} EQUAL ${SGLEN})
break()
endif()
endwhile()
endif()
endmacro()
## Subproject loader.
# aws_subproject loads a CMake subproject.
macro(aws_subproject name dir)
string(TOUPPER ${name} name2)
set("${name2}_DIRECTORY_SRC" "${PROJECT_SOURCE_DIR}/${dir}")
set("${name2}_DIRECTORY_BIN" "${PROJECT_BINARY_DIR}/${dir}")
message(${${name2}_DIRECTORY_SRC})
add_subdirectory(${${name2}_DIRECTORY_SRC})
unset(name2)
endmacro()
## Component loading shortcut.
# Hacky little helper to load something from the components directory
# without resorting to exposing the include call with variables.
#
# Allows usage in smaller things.
macro(aws_component filename)
include("${PROJECT_SOURCE_DIR}/Components/${filename}.cmake")
endmacro()
## Platform loading.
# Trivial hack to make platform loading look neater.
macro(aws_platform_init)
# Include support flagging before doing the below.
if(EXISTS "${PROJECT_PLATFORM}/PlatformSupport.cmake")
include("${PROJECT_PLATFORM}/PlatformSupport.cmake")
else()
set(AWS_PLATFORM_ALLOW_WINDOWS 1)
set(AWS_PLATFORM_ALLOW_DARWIN 1)
set(AWS_PLATFORM_ALLOW_LINUX 1)
set(AWS_PLATFORM_ALLOW_OPENBSD 0)
set(AWS_ARCH_ALLOW_AMD64 1)
set(AWS_ARCH_ALLOW_X86 1)
set(AWS_ARCH_ALLOW_ARM 0)
endif()
if(AWS_PLATFORM_ALLOW_WINDOWS)
if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set(AWS_PLATFORM_COMMON "Windows")
set(AWS_PLATFORM_WINDOWS 1)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(AWS_PLATFORM_ALIAS "Win64")
set(AWS_ARCH_AMD64 1)
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(AWS_PLATFORM_ALIAS "Win32")
set(AWS_ARCH_X86 1)
endif()
endif(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
endif(AWS_PLATFORM_ALLOW_WINDOWS)
if(AWS_PLATFORM_ALLOW_LINUX)
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(AWS_PLATFORM_COMMON "Linux")
set(AWS_PLATFORM_LINUX 1)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(AWS_PLATFORM_ALIAS "Lin64")
set(AWS_ARCH_AMD64 1)
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(AWS_PLATFORM_ALIAS "Lin32")
set(AWS_ARCH_X86 1)
endif()
endif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
endif(AWS_PLATFORM_ALLOW_LINUX)
if(AWS_PLATFORM_ALLOW_DARWIN)
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(AWS_PLATFORM_COMMON "Darwin")
set(AWS_PLATFORM_DARWIN 1)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(AWS_PLATFORM_ALIAS "Darwin")
set(AWS_ARCH_AMD64 1)
endif()
endif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
endif(AWS_PLATFORM_ALLOW_DARWIN)
if(AWS_PLATFORM_ALLOW_OPENBSD)
if(${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD")
set(AWS_PLATFORM_COMMON "OpenBSD")
set(AWS_PLATFORM_OPENBSD 1)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(AWS_PLATFORM_ALIAS "OpenBSD")
set(AWS_ARCH_AMD64 1)
endif()
endif(${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD")
endif(AWS_PLATFORM_ALLOW_OPENBSD)
if(AWS_PLATFORM_COMMON)
ELSE()
MESSAGE(FATAL_ERROR "[AWS:PLATFORM] Detected operating system and platform combination is not supported.")
endif()
endmacro()
# Copyright 2014-2017 A.W. 'aws' Stanley.
#
# Licensed under the Apache Licence, Version 2.0 [1] or
# MIT licence [2], at your option. This file may not be
# copied, modified, or distributed except according to
# those terms.
#
# [1] <http://www.apache.org/licenses/LICENSE-2.0>
# [2] <http://opensource.org/licenses/MIT>
## Unwinds one list into a source group.
# This unwinds a list into source_group and stores files in ${outvar}.
# e.g. aws_unwind(LUA_SOURCE_GROUPS ${EXE_FILES})
macro(aws_unwind list outvar)
set(SGLEN 0)
set(SGPOS 0)
list(LENGTH ${list} SGLEN)
if(NOT ${SGLEN} EQUAL ${SGPOS})
while(TRUE)
set(SGNAME "")
set(SGLISTNAME "")
set(SGLIST "")
list(GET ${list} ${SGPOS} SGNAME)
math(EXPR SGPOS "${SGPOS}+1")
if(${SGPOS} EQUAL ${SGLEN})
break()
endif()
list(GET ${list} ${SGPOS} SGLISTNAME)
math(EXPR SGPOS "${SGPOS}+1")
foreach(a ${${SGLISTNAME}})
list(APPEND SGLIST ${a})
list(APPEND ${outvar} ${a})
endforeach()
source_group(${SGNAME} FILES ${SGLIST})
if(${SGPOS} EQUAL ${SGLEN})
break()
endif()
endwhile()
endif()
endmacro()
## Subproject loader.
# aws_subproject loads a CMake subproject.
macro(aws_subproject name dir)
string(TOUPPER ${name} name2)
set("${name2}_DIRECTORY_SRC" "${PROJECT_SOURCE_DIR}/${dir}")
set("${name2}_DIRECTORY_BIN" "${PROJECT_BINARY_DIR}/${dir}")
add_subdirectory(${${name2}_DIRECTORY_SRC})
unset(name2)
endmacro()
## Component loading shortcut.
# Hacky little helper to load something from the components directory
# without resorting to exposing the include call with variables.
#
# Allows usage in smaller things.
macro(aws_component filename)
include("${PROJECT_SOURCE_DIR}/Components/${filename}.cmake")
endmacro()
## Include unwinder
macro(aws_includes target_name includes)
foreach(inc ${${SGLISTNAME}})
target_include_directories(${target_name} PRIVATE "${${inc}}")
endforeach()
endmacro()
## Platform loading.
# Trivial hack to make platform loading look neater.
macro(aws_platform_init)
# Include support flagging before doing the below.
if(EXISTS "${PROJECT_PLATFORM}/PlatformSupport.cmake")
include("${PROJECT_PLATFORM}/PlatformSupport.cmake")
else()
set(AWS_PLATFORM_ALLOW_WINDOWS 1)
set(AWS_PLATFORM_ALLOW_DARWIN 1)
set(AWS_PLATFORM_ALLOW_LINUX 1)
set(AWS_PLATFORM_ALLOW_OPENBSD 0)
set(AWS_ARCH_ALLOW_AMD64 1)
set(AWS_ARCH_ALLOW_X86 1)
set(AWS_ARCH_ALLOW_ARM 0)
endif()
if(AWS_PLATFORM_ALLOW_WINDOWS)
if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set(AWS_PLATFORM_COMMON "Windows")
set(AWS_PLATFORM_WINDOWS 1)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(AWS_PLATFORM_ALIAS "Win64")
set(AWS_ARCH_AMD64 1)
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(AWS_PLATFORM_ALIAS "Win32")
set(AWS_ARCH_X86 1)
endif()
endif(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
endif(AWS_PLATFORM_ALLOW_WINDOWS)
if(AWS_PLATFORM_ALLOW_LINUX)
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(AWS_PLATFORM_COMMON "Linux")
set(AWS_PLATFORM_LINUX 1)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(AWS_PLATFORM_ALIAS "Lin64")
set(AWS_ARCH_AMD64 1)
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
set(AWS_PLATFORM_ALIAS "Lin32")
set(AWS_ARCH_X86 1)
endif()
endif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
endif(AWS_PLATFORM_ALLOW_LINUX)
if(AWS_PLATFORM_ALLOW_DARWIN)
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(AWS_PLATFORM_COMMON "Darwin")
set(AWS_PLATFORM_DARWIN 1)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(AWS_PLATFORM_ALIAS "Darwin")
set(AWS_ARCH_AMD64 1)
endif()
endif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
endif(AWS_PLATFORM_ALLOW_DARWIN)
if(AWS_PLATFORM_ALLOW_OPENBSD)
if(${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD")
set(AWS_PLATFORM_COMMON "OpenBSD")
set(AWS_PLATFORM_OPENBSD 1)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(AWS_PLATFORM_ALIAS "OpenBSD")
set(AWS_ARCH_AMD64 1)
endif()
endif(${CMAKE_SYSTEM_NAME} MATCHES "OpenBSD")
endif(AWS_PLATFORM_ALLOW_OPENBSD)
if(AWS_PLATFORM_COMMON)
ELSE()
MESSAGE(FATAL_ERROR "[AWS:PLATFORM] Detected operating system and platform combination is not supported.")
endif()
endmacro()
#### Actual path: ${PROJECT_SOURCE_DIR}/Components/ThirdParty.cmake
# Copyright 2014-2017 A.W. 'aws' Stanley.
#
# Licensed under the Apache Licence, Version 2.0 [1] or
# MIT licence [2], at your option. This file may not be
# copied, modified, or distributed except according to
# those terms.
#
# [1] <http://www.apache.org/licenses/LICENSE-2.0>
# [2] <http://opensource.org/licenses/MIT>
# Includes Lua in a project taking a source root, a target name,
# and an on/off value for building as a dynamic library (DLL/DYLIB/SO).
macro(aws_lua source_root target_name build_lua_as_lib)
aws_subproject("lua" ${source_root})
if(build_lua_as_lib)
add_library(lua ${libmode} ${LUA_INCLUDES} ${LUA_SOURCES})
IF(${WOE_OS_COMMON} MATCHES "Windows")
target_compile_definitions(${proj} PRIVATE
"-DLUA_BUILD_AS_DLL -DLUA_LIB=1")
else()
target_compile_definitions(${proj} PRIVATE
"-DLUA_LIB=1")
endif()
endif()
list(APPEND lua_bin_files ${LUA_INCLUDES} ${LUA_SOURCES})
aws_unwind(LUA_SOURCE_GROUPS ${lua_bin_files})
unset(LUA_INCLUDES)
unset(LUA_SOURCES)
unset(LUA_SOURCE_GROUPS)
target_include_directories(${target_name} PRIVATE "${LUA_INCLUDE_DIRECTORY}")
if(build_lua_as_lib)
add_dependencies(${target_name} lua)
target_link_libraries(${target_name} lua)
else()
target_sources(${target_name} PUBLIC ${lua_bin_files})
endif()
unset(lua_bin_files)
if(build_lua_as_lib)
message("[External] Added Lua to the project as a ${libmode} library.")
else()
message("[External] Added Lua to the project.")
endif()
endmacro()
#### Actual path: ${source_root}
# Source root varies based on offsets. Sits above 'src' from the tarball.
# Copyright 2014-2017 A.W. 'aws' Stanley.
#
# Licensed under the Apache Licence, Version 2.0 [1] or
# MIT licence [2], at your option. This file may not be
# copied, modified, or distributed except according to
# those terms.
#
# [1] <http://www.apache.org/licenses/LICENSE-2.0>
# [2] <http://opensource.org/licenses/MIT>
SET(LUA_INCLUDE_DIRECTORY
"${CMAKE_CURRENT_SOURCE_DIR}/src"
CACHE INTERNAL "" FORCE
)
SET(LUA_INCLUDES
"${CMAKE_CURRENT_SOURCE_DIR}/src/lapi.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lauxlib.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lcode.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lctype.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/ldebug.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/ldo.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lfunc.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lgc.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/llex.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/llimits.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lmem.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lobject.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lopcodes.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lparser.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lprefix.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lstate.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lstring.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/ltable.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/ltm.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lua.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/luaconf.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lualib.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lundump.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lvm.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lzio.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lua.hpp"
CACHE INTERNAL "" FORCE
)
SET(LUA_SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/src/lapi.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lauxlib.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lbaselib.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lbitlib.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lcode.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lcorolib.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lctype.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/ldblib.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/ldebug.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/ldo.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/ldump.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lfunc.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lgc.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/linit.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/liolib.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/llex.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lmathlib.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lmem.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/loadlib.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lobject.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lopcodes.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/loslib.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lparser.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lstate.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lstring.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lstrlib.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/ltable.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/ltablib.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/ltm.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lundump.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lutf8lib.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lvm.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/lzio.c"
#"${CMAKE_CURRENT_SOURCE_DIR}/src/lua.c"
#"${CMAKE_CURRENT_SOURCE_DIR}/src/luac.c"
CACHE INTERNAL "" FORCE
)
# Must be double escaped
# Use CACHE INTERNAL "" FORCE to ensure depth is irrelevant.
SET(LUA_SOURCE_GROUPS
"Header Files\\\\Lua (Official Sources)" LUA_INCLUDES
"Source Files\\\\Lua (Official Sources)" LUA_SOURCES
CACHE INTERNAL "" FORCE
)
#### Actual path: ${PROJECT_SOURCE_DIR}/Components/ThirdParty.cmake
# Copyright 2014-2017 A.W. 'aws' Stanley.
#
# Licensed under the Apache Licence, Version 2.0 [1] or
# MIT licence [2], at your option. This file may not be
# copied, modified, or distributed except according to
# those terms.
#
# [1] <http://www.apache.org/licenses/LICENSE-2.0>
# [2] <http://opensource.org/licenses/MIT>
#Lua
aws_component("Lua")
aws_lua(
# Source root
"${PROJECT_DIRECTORY_THIRDPARTY}/lua"
# Target name is binary_name
${binary_name}
# Build as library
off
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment