-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
142 lines (111 loc) · 4.84 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
cmake_minimum_required(VERSION 3.3)
project(Snake)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -pedantic -static -fopenmp -Wall")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake_modules")
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if(NOT SFML_ROOT)
set(SFML_ROOT CACHE "" FORCE)
endif()
find_package(OpenGL REQUIRED)
if (OPENGL_FOUND)
include_directories(${OPENDGL_INCLUDE_DIR})
endif()
#set(GLEW_INCLUDE_DIR "C:/dev/glew-2.1.0/include")
#set(GLEW_LIBRARY "C:/dev/glew-2.1.0/lib" )
find_package(GLEW REQUIRED)
if (GLEW_FOUND)
include_directories(${GLEW_INCLUDE_DIR})
endif()
if(NOT SFML_ROOT)
set(SFML_ROOT "C:/SFML-2.4.2/build/install")
endif()
#find_package(JPEG REQUIRED)
#include_directories(${JPEG_INCLUDE_DIR})
set(SFML_STATIC_LIBRARIES TRUE)
find_package(SFML 2 REQUIRED window graphics audio network system)
if (NOT SFML_FOUND)
message(FATAL_ERROR "SFML couldn't be located!")
endif()
#message(STATUS "Dep: ${SFML_DEPENDENCIES}")
include_directories(${SFML_INCLUDE_DIR})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_FLAGS}")
file(GLOB_RECURSE src_files #add all the cpp's in src
src/*.cpp
)
file(GLOB_RECURSE c_res_files
res/*.c
)
#set(src_files src/main.cpp src/Game.cpp src/States/Playing.cpp) #src/ResourceManager/ResourceHolder.cpp)
message("----- Cpp's: ------")
foreach(file_path ${src_files})
message(${file_path})
endforeach()
file(GLOB_RECURSE hdr_files #add all the headers in src
src/*.h
)
#message("----- Headers: ------")
set(HDR_DIRS "") #list to keep include dir's
foreach(file ${hdr_files})
get_filename_component(file_path ${file} PATH)
set(HDR_DIRS ${HDR_DIRS} ${file_path})
#message(${file})
endforeach()
list(REMOVE_DUPLICATES HDR_DIRS)
include_directories("${HDR_DIRS}") #include the headers
include_directories("./res/")
message("----------------------INC DIRS:----------------")
get_property(inc_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
message("// " "${inc_dirs}" " -- //")
message("-----------------LINKS:---------------")
message("// " "${SFML_LIBRARIES} ${SFML_DEPENDENCIES} ${PTHREAD_LIBRARIES} ${JPEG_LIBRARY}" " -- //")
add_executable(main ${src_files} ${c_res_files})
#set_target_properties(main PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_link_libraries(main
${SFML_LIBRARIES}
${SFML_DEPENDENCIES}
${PTHREAD_LIBRARIES})
#Install executable
install(TARGETS main
RUNTIME DESTINATION .
)
#EMBED RESOURCES INTO THE BINNARY -- very hacky TODO make nicer
# Creates C resources file from files in given directory (provides the variables in includes)
function(create_resources dir output includes)
# Create empty output file
file(WRITE ${output} "")
# Collect input files
file(GLOB bins ${dir}/*)
# Iterate through input files
set(EXTERNS "")
set(PAIRS "")
foreach(bin ${bins})
# Get short filename
string(REGEX MATCH "([^/]+)$" filename ${bin})
# Replace filename spaces & extension separator for C compatibility
string(REGEX REPLACE "\\.| |-" "_" filename ${filename})
# Read hex data from file
file(READ ${bin} filedata HEX)
# Convert hex data for C compatibility
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," filedata ${filedata})
# Append data to output file
file(APPEND ${output} "const unsigned char ${filename}[] = {${filedata}0x00};\nconst unsigned ${filename}_size = sizeof(${filename});\n")
set(EXTERNS "${EXTERNS}extern \"C\" const char ${filename}[];\nextern \"C\" const unsigned ${filename}_size;\n")
set(PAIRS "${PAIRS} {\"${filename}\", std::make_pair(static_cast<const void*>(&${filename}), static_cast<const std::size_t>(${filename}_size) )},\n")
endforeach()
get_filename_component(FILEN ${output} NAME)
file (APPEND ${includes} "${EXTERNS}")
file (APPEND "./build/temp.h" ${PAIRS})
endfunction()
file (WRITE "./src/ResourceManager/includes.h" "//auto generated by CMake script\n#pragma once\n\n#include <unordered_map>\n\n") #clear all the previous inludes
# embades the resources into the binary
file (WRITE "./build/temp.h" "") #hacky solution
create_resources("./res/fonts/" "./res/fonts.c" "./src/ResourceManager/includes.h")
create_resources("./res/sfx/" "./res/sfx.c" "./src/ResourceManager/includes.h")
create_resources("./res/textures/" "./res/textures.c" "./src/ResourceManager/includes.h")
file(READ "./build/temp.h" PAIRS)
file(REMOVE "./build/temp.h") #remove the file that we used to fix problems wth variables (because of concurrency of the build)
file(APPEND "./src/ResourceManager/includes.h" "\ninline void initializeDataSizePairs(std::unordered_map<std::string, std::pair<const void*, const std::size_t>> & dataSizePairs)\n{\n dataSizePairs = {\n${PAIRS}\n };\n}")
#install(DIRECTORY ./res/
# DESTINATION ./res/
#)