Skip to content

Commit 2cf7f70

Browse files
committed
Various fixes to make things work in Visual Studio
1 parent bcf8a99 commit 2cf7f70

File tree

8 files changed

+37634
-17
lines changed

8 files changed

+37634
-17
lines changed

CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@ cmake_minimum_required(VERSION 3.1)
22

33
project(libvoxelbot)
44

5+
include("${CMAKE_CURRENT_SOURCE_DIR}/bin2sh.cmake.txt")
6+
bin2h(SOURCE_FILE "${CMAKE_CURRENT_SOURCE_DIR}/libvoxelbot/generated/units.data" HEADER_FILE "${CMAKE_CURRENT_SOURCE_DIR}/libvoxelbot/generated/units_data.h" VARIABLE_NAME "LIBVOXELBOT_DATA_UNITS")
7+
bin2h(SOURCE_FILE "${CMAKE_CURRENT_SOURCE_DIR}/libvoxelbot/generated/upgrades.bin" HEADER_FILE "${CMAKE_CURRENT_SOURCE_DIR}/libvoxelbot/generated/upgrades_data.h" VARIABLE_NAME "LIBVOXELBOT_DATA_UPGRADES")
8+
bin2h(SOURCE_FILE "${CMAKE_CURRENT_SOURCE_DIR}/libvoxelbot/generated/abilities.bin" HEADER_FILE "${CMAKE_CURRENT_SOURCE_DIR}/libvoxelbot/generated/abilities_data.h" VARIABLE_NAME "LIBVOXELBOT_DATA_ABILITIES")
9+
10+
511
add_subdirectory("cereal")
612
add_subdirectory("s2client-api")
713

14+
815
# PYTHON_EXECUTABLE
916

1017
# Use bin as the directory for all executables.
@@ -65,6 +72,8 @@ set(libvoxelbot_sources
6572
"libvoxelbot/caching/dependency_analyzer.cpp"
6673
)
6774

75+
76+
6877
add_library(libvoxelbot ${libvoxelbot_sources})
6978

7079
# Sets the grouping in IDEs like visual studio (last parameter is the group name)
@@ -89,6 +98,7 @@ target_include_directories(libvoxelbot
8998
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/s2client-api/examples/common> # TODO: Remove?
9099
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/s2client-api/contrib/SDL-mirror/include>
91100
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/cereal/include>
101+
PRIVATE
92102
src/generated
93103
)
94104

@@ -104,4 +114,4 @@ create_executable(test_build_optimizer "libvoxelbot/buildorder/optimizer.test.cp
104114
create_executable(cache_mappings "libvoxelbot/caching/caching.cpp")
105115
create_executable(example_combat_simulator "examples/combat_simulator.cpp")
106116
create_executable(example_combat_simulator2 "examples/combat_simulator2.cpp")
107-
create_executable(example_build_optimizer "examples/build_optimizer.cpp")
117+
create_executable(example_build_optimizer "examples/build_optimizer.cpp")

bin2sh.cmake.txt

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
include(CMakeParseArguments)
3+
4+
# Function to wrap a given string into multiple lines at the given column position.
5+
# Parameters:
6+
# VARIABLE - The name of the CMake variable holding the string.
7+
# AT_COLUMN - The column position at which string will be wrapped.
8+
function(WRAP_STRING)
9+
set(oneValueArgs VARIABLE AT_COLUMN)
10+
cmake_parse_arguments(WRAP_STRING "${options}" "${oneValueArgs}" "" ${ARGN})
11+
12+
string(LENGTH ${${WRAP_STRING_VARIABLE}} stringLength)
13+
math(EXPR offset "0")
14+
15+
while(stringLength GREATER 0)
16+
17+
if(stringLength GREATER ${WRAP_STRING_AT_COLUMN})
18+
math(EXPR length "${WRAP_STRING_AT_COLUMN}")
19+
else()
20+
math(EXPR length "${stringLength}")
21+
endif()
22+
23+
string(SUBSTRING ${${WRAP_STRING_VARIABLE}} ${offset} ${length} line)
24+
set(lines "${lines}\n${line}")
25+
26+
math(EXPR stringLength "${stringLength} - ${length}")
27+
math(EXPR offset "${offset} + ${length}")
28+
endwhile()
29+
30+
set(${WRAP_STRING_VARIABLE} "${lines}" PARENT_SCOPE)
31+
endfunction()
32+
# Function to embed contents of a file as byte array in C/C++ header file(.h). The header file
33+
# will contain a byte array and integer variable holding the size of the array.
34+
# Parameters
35+
# SOURCE_FILE - The path of source file whose contents will be embedded in the header file.
36+
# VARIABLE_NAME - The name of the variable for the byte array. The string "_SIZE" will be append
37+
# to this name and will be used a variable name for size variable.
38+
# HEADER_FILE - The path of header file.
39+
# APPEND - If specified appends to the header file instead of overwriting it
40+
# NULL_TERMINATE - If specified a null byte(zero) will be append to the byte array. This will be
41+
# useful if the source file is a text file and we want to use the file contents
42+
# as string. But the size variable holds size of the byte array without this
43+
# null byte.
44+
# Usage:
45+
# bin2h(SOURCE_FILE "Logo.png" HEADER_FILE "Logo.h" VARIABLE_NAME "LOGO_PNG")
46+
function(BIN2H)
47+
set(options APPEND NULL_TERMINATE)
48+
set(oneValueArgs SOURCE_FILE VARIABLE_NAME HEADER_FILE)
49+
cmake_parse_arguments(BIN2H "${options}" "${oneValueArgs}" "" ${ARGN})
50+
51+
# reads source file contents as hex string
52+
file(READ ${BIN2H_SOURCE_FILE} hexString HEX)
53+
string(LENGTH ${hexString} hexStringLength)
54+
55+
# appends null byte if asked
56+
if(BIN2H_NULL_TERMINATE)
57+
set(hexString "${hexString}00")
58+
endif()
59+
60+
# wraps the hex string into multiple lines at column 32(i.e. 16 bytes per line)
61+
wrap_string(VARIABLE hexString AT_COLUMN 32)
62+
math(EXPR arraySize "${hexStringLength} / 2")
63+
64+
# adds '0x' prefix and comma suffix before and after every byte respectively
65+
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1, " arrayValues ${hexString})
66+
# removes trailing comma
67+
string(REGEX REPLACE ", $" "" arrayValues ${arrayValues})
68+
69+
# converts the variable name into proper C identifier
70+
string(MAKE_C_IDENTIFIER "${BIN2H_VARIABLE_NAME}" BIN2H_VARIABLE_NAME)
71+
string(TOUPPER "${BIN2H_VARIABLE_NAME}" BIN2H_VARIABLE_NAME)
72+
73+
# declares byte array and the length variables
74+
set(arrayDefinition "const unsigned char ${BIN2H_VARIABLE_NAME}[] = { ${arrayValues} };")
75+
set(arraySizeDefinition "const size_t ${BIN2H_VARIABLE_NAME}_SIZE = ${arraySize};")
76+
77+
set(declarations "${arrayDefinition}\n\n${arraySizeDefinition}\n\n")
78+
if(BIN2H_APPEND)
79+
file(APPEND ${BIN2H_HEADER_FILE} "${declarations}")
80+
else()
81+
file(WRITE ${BIN2H_HEADER_FILE} "${declarations}")
82+
endif()
83+
endfunction()

build.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#/bin/bash
2+
3+
rm -rf precompiled_lib
4+
mkdir precompiled_lib
5+
cd precompiled_lib
6+
mkdir lib
7+
cp ../build/Release/libvoxelbot.lib lib/libvoxelbot_release.lib
8+
cp ../build/Release/libvoxelbot.lib lib/libvoxelbot_debug.lib
9+
mkdir include
10+
cp -r ../libvoxelbot include
11+
find include -name "*.cpp" | xargs rm
12+
rm -rf include/libvoxelbot/generated
13+
cp -r ../cereal/include/cereal include
14+
15+
# cd build
16+
# cmake -DCMAKE_INSTALL_PREFIX:PATH=../precompiled_lib .. && make all install

0 commit comments

Comments
 (0)