-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbuild.sh
More file actions
179 lines (145 loc) · 6.78 KB
/
Copy pathbuild.sh
File metadata and controls
179 lines (145 loc) · 6.78 KB
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
#!/bin/bash
# Exit immediately if a command exits with a non-zero status.
set -e
################################################################################
# CONFIGURATION
################################################################################
# Compiler and flags
CXX="g++" # C++ compiler
CXXFLAGS=("-std=c++17" "-O2" -w) # Using C++17, with optimizations and warnings "-Wall" "-Wextra"
CC="gcc" # C compiler
CFLAGS=("-std=c11" "-O2" "-Wall" "-Wextra") # C compilation flags (e.g., C11 standard)
# Project directories
ROOT_DIR=$(pwd)
BUILD_DIR="${ROOT_DIR}/build"
DEPS_BUILD_DIR="${BUILD_DIR}/deps_install"
# libtcod configuration
LIBTCOD_SRC_DIR="${ROOT_DIR}/libtcod"
# modaccent configuration
MODACCENT_SRC_DIR="${ROOT_DIR}/modaccent"
# Incursion source files (adjust this to match your project)
INCURSION_SRC_FILES=$(find src -name "*.cpp")
INCURSION_EXE_NAME="incursion"
################################################################################
# HELPER FUNCTIONS
################################################################################
# A function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
################################################################################
# BUILD STEPS
################################################################################
build_dependencies() {
echo "--- Building Dependencies ---"
mkdir -p "${DEPS_BUILD_DIR}/lib" # For compiled libraries
mkdir -p "${DEPS_BUILD_DIR}/include" # For library headers
# --- SDL2 ---
echo "[Dep] Checking for SDL2..."
# We check if the development package is installed.
if pkg-config --exists sdl2; then
echo "SDL2 development library found via pkg-config."
else
echo "WARNING: SDL2 development library not found."
echo "Please install it using your system's package manager."
echo "Example for Debian/Ubuntu: sudo apt-get install libsdl2-dev"
exit 1
fi
# --- libtcod ---
echo "[Dep] Checking for libtcod..."
if [ ! -f "${DEPS_BUILD_DIR}/lib/libtcod.a" ]; then
echo "libtcod not found in build directory. Building from source..."
if [ ! -d "${LIBTCOD_SRC_DIR}" ]; then
echo "ERROR: libtcod source directory not found at ${LIBTCOD_SRC_DIR}"
echo "Please ensure the libtcod source is present in your project."
exit 1
fi
echo "Building libtcod via direct compilation from local source..."
cd "${LIBTCOD_SRC_DIR}"
# Find all C and C++ source files needed for the library.
LIBTCOD_SOURCES=$(find src -name "*.c" -o -name "*.cpp")
OBJECTS_DIR="build/objects"
mkdir -p "${OBJECTS_DIR}"
# Compile all source files into object files (.o).
# -fPIC is required for code that will be linked into a library.
# We need to include the libtcod 'src' dir and the system SDL2 headers.
echo "Compiling libtcod source files (mixed C/C++)..."
# Base flags common to both C and C++ compilation for libtcod
LIBTCOD_BASE_FLAGS=("-O2" "-g" "-Wall" "-fPIC" "-DTCOD_SDL2" "-DNO_OPENGL" $(pkg-config --cflags sdl2) "-Iinclude")
OBJECT_FILES=()
for SRC_FILE in ${LIBTCOD_SOURCES}; do
# Determine compiler and flags based on file extension
COMPILER=""
COMPILE_FLAGS=()
if [[ "${SRC_FILE}" == *.c ]]; then
COMPILER="${CC}"
COMPILE_FLAGS=("${CFLAGS[@]}" "${LIBTCOD_BASE_FLAGS[@]}")
elif [[ "${SRC_FILE}" == *.cpp ]]; then
COMPILER="${CXX}"
COMPILE_FLAGS=("${CXXFLAGS[@]}" "${LIBTCOD_BASE_FLAGS[@]}" "-fpermissive") # -fpermissive is often C++ specific
else
echo "WARNING: Unknown source file type for libtcod: ${SRC_FILE}. Skipping."
continue
fi
# Create a unique object file path by replacing slashes with underscores to avoid name collisions.
OBJ_FILE="${OBJECTS_DIR}/$(echo "${SRC_FILE}" | tr '/' '_').o"
echo " - Compiling ${SRC_FILE} with ${COMPILER}"
"${COMPILER}" "${COMPILE_FLAGS[@]}" -c "${SRC_FILE}" -o "${OBJ_FILE}"
OBJECT_FILES+=("${OBJ_FILE}")
done
# Archive all the object files into a single static library (libtcod.a).
echo "Archiving object files into libtcod.a..."
ar rcs "${DEPS_BUILD_DIR}/lib/libtcod.a" "${OBJECT_FILES[@]}"
# Copy the public headers to our dependency installation directory.
echo "Installing libtcod headers..."
cp -r include/* "${DEPS_BUILD_DIR}/include/"
cd "${ROOT_DIR}" # Return to the project root
echo "libtcod build complete."
else
echo "libtcod is already built."
fi
if [ ! -f "${BUILD_DIR}/modaccent" ]; then
echo "Building modaccent..."
cd "${MODACCENT_SRC_DIR}"
"${CC}" "${CFLAGS[@]}" -Iinclude -o "${BUILD_DIR}/modaccent" $(find src -name "*.c")
cd "${ROOT_DIR}" # Return to the project root
else
echo "modaccent is already built."
fi
}
build_incursion() {
# echo "--- Running flex ---"
# rm src/Tokens.cpp
# flex -B -i -osrc/Tokens.cpp lang/Tokens.lex
# echo "--- Running modaccent ---"
# rm src/yygram.cpp inc/yygram.h
# ${BUILD_DIR}/modaccent lang/Grammar.acc src inc
echo "--- Building Incursion ---"
mkdir -p "${BUILD_DIR}/incursion"
# Add include paths for our dependencies
# SDL2 flags are retrieved using pkg-config for simplicity
# We add our locally-built libtcod paths manually
INCLUDE_PATHS="-Iinc -Ilib -I${DEPS_BUILD_DIR}/include $(pkg-config --cflags sdl2)"
# Add library paths and libraries to link
LINKER_PATHS="-L${DEPS_BUILD_DIR}/lib"
LIBRARIES="$(pkg-config --libs sdl2) -ltcod"
echo "Compiling and linking Incursion..."
echo "Source files: ${INCURSION_SRC_FILES}"
echo "Compiling with CXXFLAGS: ${CXXFLAGS[*]}"
${CXX} "${CXXFLAGS[@]}" -DLIBTCOD_TERM ${INCLUDE_PATHS} ${LINKER_PATHS} \
-o "${BUILD_DIR}/incursion/${INCURSION_EXE_NAME}" \
${INCURSION_SRC_FILES} \
${LIBRARIES}
echo "--- Build successful! ---"
echo "Executable is at: ${BUILD_DIR}/incursion/${INCURSION_EXE_NAME}"
}
################################################################################
# SCRIPT MAIN LOGIC
################################################################################
# Check for required tools
command_exists ${CXX} || { echo "${CXX} is not installed. Please install g++."; exit 1; }
command_exists pkg-config || { echo "pkg-config is not installed. Please install it."; exit 1; }
command_exists ar || { echo "ar (from binutils) is not installed. Please install build-essential."; exit 1; }
build_dependencies
build_incursion
echo "All tasks complete."