-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
205 lines (157 loc) · 4.15 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
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
#
# Project
#
project(palantir)
#
# Settings
#
enable_testing()
#
# Imports
#
include(CheckFunctionExists)
include(CheckLibraryExists)
#
# Prerequisites
#
find_package(Git REQUIRED)
find_package(Lua REQUIRED)
find_program(LUA_EXECUTABLE NAMES lua)
find_program(LUAC_EXECUTABLE NAMES luac)
if(NOT LUA_EXECUTABLE)
message(FATAL_ERROR "Lua not found!")
endif(NOT LUA_EXECUTABLE)
if(NOT LUAC_EXECUTABLE)
message(FATAL_ERROR "Luac not found!")
endif(NOT LUAC_EXECUTABLE)
if(NOT UNIX AND NOT WIN32)
message(FATAL_ERROR "System not supported!")
endif(NOT UNIX AND NOT WIN32)
#
# Variables
#
execute_process(COMMAND ${GIT_EXECUTABLE} describe --abbrev=0 --tags
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
OUTPUT_VARIABLE GIT_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(REGEX REPLACE "^v" "" GIT_VERSION ${GIT_VERSION})
string(CONCAT VERSION "Palantir" " " ${GIT_VERSION})
message(STATUS "Using version \"${VERSION}\"")
#
# Compiler flags
#
set(BUILD "${CMAKE_SYSTEM} ${CMAKE_C_COMPILER_ID}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -pedantic -Wall -Werror")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DVERSION='\"${VERSION}\"'")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DBUILD='\"${BUILD}\"'")
#
# Source files
#
set(SOURCE_FILES
${CMAKE_SOURCE_DIR}/src/lib/args.c
${CMAKE_SOURCE_DIR}/src/lib/args.h
${CMAKE_SOURCE_DIR}/src/lib/crc.c
${CMAKE_SOURCE_DIR}/src/lib/crc.h
${CMAKE_SOURCE_DIR}/src/lib/net.c
${CMAKE_SOURCE_DIR}/src/lib/net.h
${CMAKE_SOURCE_DIR}/src/lib/os.h
${CMAKE_SOURCE_DIR}/src/lua/lua.c
${CMAKE_SOURCE_DIR}/src/lua/lua.h
${CMAKE_SOURCE_DIR}/src/palantir.c
${CMAKE_SOURCE_DIR}/src/palantir.h
${CMAKE_SOURCE_DIR}/src/palantir.lua
)
#
# Libraries
#
set(LIBRARIES "")
#
# Debug mode
#
option(DEBUG "Debug mode" OFF)
if(DEBUG)
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDEBUG=1")
endif(DEBUG)
#
# POSIX support
#
if(UNIX)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_DEFAULT_SOURCE")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_GNU_SOURCE")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_BSD_SOURCE")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DPOSIX=1")
set(SOURCE_FILES "${SOURCE_FILES}"
${CMAKE_SOURCE_DIR}/src/lib/os_posix.c
)
set(LIBRARIES "${LIBRARIES}" dl)
endif(UNIX)
#
# Windows NT support
#
if(WIN32)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DWINNT=1")
set(SOURCE_FILES "${SOURCE_FILES}"
${CMAKE_SOURCE_DIR}/src/lib/os_winnt.c
)
set(LIBRARIES "${LIBRARIES}" ws2_32)
endif(WIN32)
#
# Daemon support (Excluding OS X)
#
check_function_exists(daemon HAVE_DAEMON)
if(HAVE_DAEMON AND NOT APPLE)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DHAVE_DAEMON=1")
endif(HAVE_DAEMON AND NOT APPLE)
#
# Readline support (Excluding OS X)
#
check_library_exists(readline readline "readline.h" HAVE_READLINE)
if(HAVE_READLINE AND NOT APPLE)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DHAVE_READLINE=1")
set(SOURCE_FILES "${SOURCE_FILES}"
${CMAKE_SOURCE_DIR}/src/lib/dict.h
${CMAKE_SOURCE_DIR}/src/lib/readline.c
${CMAKE_SOURCE_DIR}/src/lib/readline.h
)
set(LIBRARIES "${LIBRARIES}" readline)
endif(HAVE_READLINE AND NOT APPLE)
#
# Includes
#
include_directories(${LUA_INCLUDE_DIR} ${CMAKE_BINARY_DIR})
#
# Binary
#
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
#
# Resources
#
set(XXD "${CMAKE_SOURCE_DIR}/src/tools/xxd.lua")
set(PALANTIR_LUA "${CMAKE_SOURCE_DIR}/src/palantir.lua")
set(PALANTIR_LUAC "${CMAKE_BINARY_DIR}/palantir.luac")
set(PALANTIR_INC "${CMAKE_BINARY_DIR}/palantir.inc")
add_custom_target(resources
COMMAND ${LUAC_EXECUTABLE} -s -o ${PALANTIR_LUAC} ${PALANTIR_LUA}
COMMAND ${LUA_EXECUTABLE} ${XXD} ${PALANTIR_LUAC} > ${PALANTIR_INC}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
add_dependencies(${PROJECT_NAME} resources)
#
# Linking
#
target_link_libraries(${PROJECT_NAME} ${LUA_LIBRARIES} ${LIBRARIES})
#
# Testing (POSIX only)
#
if(UNIX AND HAVE_READLINE)
add_test(${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/test/test.sh)
endif(UNIX AND HAVE_READLINE)
#
# Install (POSIX only)
#
if(UNIX)
install(TARGETS ${PROJECT_NAME} DESTINATION bin)
endif(UNIX)