Skip to content

Commit 00e1233

Browse files
authored
Merge pull request #1 from bialger/dev
Add Boost
2 parents e71a141 + e4a72d0 commit 00e1233

7 files changed

Lines changed: 137 additions & 19 deletions

File tree

.github/workflows/ci_tests.yml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,33 +45,33 @@ jobs:
4545
- name: Build main target
4646
shell: bash
4747
run: |
48-
cmake --build cmake-build-release --target QtCMake
48+
cmake --build cmake-build-release --target QtBoostCMake
4949
5050
- name: Run program
5151
shell: bash
5252
working-directory: ./cmake-build-release/bin
5353
run: |
5454
if [ "$RUNNER_OS" == "Windows" ]; then
55-
./QtCMake.exe --help
55+
./QtBoostCMake.exe --help
5656
else
57-
./QtCMake --help
57+
./QtBoostCMake --help
5858
fi
5959
- name: Build tests
6060
shell: bash
6161
run: |
6262
if [ "$RUNNER_OS" == "Windows" ]; then
63-
cmake --build ./cmake-build-release --target QtCMake_tests || echo "Built with errors"
63+
cmake --build ./cmake-build-release --target QtBoostCMake_tests || echo "Built with errors"
6464
else
65-
cmake --build ./cmake-build-release --target QtCMake_tests
65+
cmake --build ./cmake-build-release --target QtBoostCMake_tests
6666
fi
6767
- name: Run tests
6868
shell: bash
6969
working-directory: ./cmake-build-release/tests
7070
run: |
7171
if [ "$RUNNER_OS" == "Windows" ]; then
72-
./QtCMake_tests.exe || echo "Tests failed" # Due to a specific MinGW-related Github Actions issue
72+
./QtBoostCMake_tests.exe || echo "Tests failed" # Due to a specific MinGW-related Github Actions issue
7373
else
74-
./QtCMake_tests
74+
./QtBoostCMake_tests
7575
fi
7676
7777
style-check:
@@ -158,11 +158,12 @@ jobs:
158158
sudo apt-get update
159159
# Install a C++23-capable toolchain and clang-tidy-19 for better C++23 support
160160
sudo apt-get -y install clang-19 clang-tidy-19
161-
- name: Install Qt dependencies for Linux
161+
- name: Install Qt and Boost
162162
run: |
163163
sudo apt update
164164
sudo apt install libgl1-mesa-dev libglu1-mesa-dev
165165
sudo apt install qt6-base-dev
166+
sudo apt install libboost-all-dev
166167
167168
- name: Create CMake cache
168169
run: |

CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
cmake_minimum_required(VERSION 3.25)
22

33
project(
4-
QtCMake
5-
VERSION 1.1.0
6-
DESCRIPTION "CMake project for easy Qt connection to other CMake projects"
4+
QtBoostCMake
5+
VERSION 1.0.0
6+
DESCRIPTION "CMake project for easy Qt and Boost connection to other CMake projects"
77
LANGUAGES CXX
88
)
99

@@ -29,7 +29,7 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
2929
set(CMAKE_CXX_FLAGS_DEBUG "/MDd")
3030
set(CMAKE_CXX_FLAGS_RELEASE "/O2")
3131
else ()
32-
set(CMAKE_CXX_FLAGS_DEBUG "-g")
32+
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
3333
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
3434
if (WIN32)
3535
set(CMAKE_CXX_STANDARD_LIBRARIES "-static-libgcc -static-libstdc++ -lwsock32 -lws2_32 ${CMAKE_CXX_STANDARD_LIBRARIES}")

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
# QtCMake
2-
A C++ project template with Qt6 and CMake that downloads and compiles Qt.
3-
The resulting app is an executable that can be run on a system without Qt.
1+
# QtBoostCMake
2+
3+
A C++ project template with Qt6, Boost, Google Tests and CMake that downloads and compiles Qt and Boost.
4+
The resulting app is an executable that can be run on a system without Qt and Boost.
45

56
The main Qt6 configuration is found in the [correspondent](lib/qt/CMakeLists.txt) CMakeLists.txt file.
7+
The main Boost configuration is found in the [correspondent](lib/boost/CMakeLists.txt) CMakeLists.txt file.
8+
The main Google Tests configuration is found in the [correspondent](tests/CMakeLists.txt) CMakeLists.txt file.
69

710
> Note that statically compiled Qt6 weights more than 5 GB.
811
> The good thing is that user does not need whole compiled Qt6 with this configuration.

bin/CMakeLists.txt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
1+
set(Boost_NO_SYSTEM_PATHS ON)
2+
set(Boost_USE_MULTITHREADED ON)
3+
set(Boost_USE_STATIC_LIBS ON)
4+
set(Boost_USE_STATIC_RUNTIME ON)
5+
set(BOOST_ALL_DYN_LINK OFF)
6+
7+
if (NOT Boost_ROOT AND EXISTS $ENV{BOOSTROOT})
8+
set(Boost_ROOT $ENV{BOOSTROOT})
9+
endif()
210

3-
message(STATUS "Qt6 directory: $ENV{QTDIR}")
4-
set(QT_BIN_DIR "$ENV{QTDIR}/bin")
11+
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
12+
find_package(Boost REQUIRED)
513

614
if (WIN32)
715
add_executable(${PROJECT_NAME} WIN32
@@ -13,4 +21,4 @@ else ()
1321
)
1422
endif ()
1523

16-
target_link_libraries(${PROJECT_NAME} PRIVATE qt)
24+
target_link_libraries(${PROJECT_NAME} PRIVATE qt boost)

bin/main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <iostream>
22

3+
#include <boost/filesystem.hpp>
4+
35
#include <QApplication>
46
#include <QDateTime>
57
#include <QLCDNumber>
@@ -14,6 +16,9 @@ static constexpr const char* kFormat = "hh:mm:ss";
1416
} // namespace
1517

1618
int main(int argc, char** argv) {
19+
// Boost.Filesystem (requires linking)
20+
std::cout << "CWD: " << boost::filesystem::current_path().string() << "\n";
21+
1722
if (argc > 1) {
1823
std::cout << "No CLI arguments allowed.\n";
1924
return 0;

lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ cmake_minimum_required(VERSION 3.25)
22

33
if(NOT TESTS_ONLY)
44
add_subdirectory(qt)
5+
add_subdirectory(boost)
56
endif()

lib/boost/CMakeLists.txt

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Error if building out of a build directory
2+
file(TO_CMAKE_PATH "${PROJECT_BINARY_DIR}/CMakeLists.txt" LOC_PATH)
3+
if(EXISTS "${LOC_PATH}")
4+
message(FATAL_ERROR "You cannot build in a source directory (or any directory with "
5+
"CMakeLists.txt file). Please make a build subdirectory. Feel free to "
6+
"remove CMakeCache.txt and CMakeFiles.")
7+
endif()
8+
9+
set(BOOST_VERSION "1.83.0")
10+
set(BOOST_VERSION_UNDERSCORE "1_83_0")
11+
set(BOOST_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/boost_${BOOST_VERSION_UNDERSCORE}")
12+
# b2 stage (default) puts libs in <source>/stage/lib or stage/lib/<variant>/, headers stay in <source>
13+
set(BOOST_STAGE_DIR "${BOOST_SOURCE_DIR}/stage")
14+
set(BOOST_STAGE_LIB_DIR "${BOOST_SOURCE_DIR}/stage/lib")
15+
set(ENV{BOOSTROOT} ${BOOST_SOURCE_DIR})
16+
17+
# If we already built Boost here (stage), use it directly and skip find_package/build
18+
if(EXISTS "${BOOST_SOURCE_DIR}/boost/version.hpp")
19+
if(EXISTS "${BOOST_STAGE_DIR}")
20+
set(Boost_ROOT ${BOOST_SOURCE_DIR})
21+
endif()
22+
endif()
23+
24+
find_package(Boost 1.83 QUIET)
25+
26+
if(NOT Boost_FOUND)
27+
message(STATUS "Boost not found. Downloading and building Boost...")
28+
29+
set(BOOST_ARCHIVE_URL "https://archives.boost.io/release/${BOOST_VERSION}/source/boost_${BOOST_VERSION_UNDERSCORE}.tar.gz")
30+
set(BOOST_ARCHIVE_FILE "${CMAKE_CURRENT_BINARY_DIR}/boost_${BOOST_VERSION_UNDERSCORE}.tar.gz")
31+
32+
message(STATUS "Downloading Boost from archives.boost.io...")
33+
file(DOWNLOAD ${BOOST_ARCHIVE_URL} ${BOOST_ARCHIVE_FILE}
34+
SHOW_PROGRESS)
35+
36+
message(STATUS "Unpacking the archive...")
37+
execute_process(COMMAND ${CMAKE_COMMAND} -E tar xvf ${BOOST_ARCHIVE_FILE}
38+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
39+
OUTPUT_QUIET
40+
)
41+
42+
message(STATUS "Running bootstrap...")
43+
if(WIN32)
44+
execute_process(COMMAND "${BOOST_SOURCE_DIR}/bootstrap.bat" gcc
45+
WORKING_DIRECTORY ${BOOST_SOURCE_DIR})
46+
set(B2_EXECUTABLE "${BOOST_SOURCE_DIR}/b2.exe")
47+
else()
48+
execute_process(COMMAND "${BOOST_SOURCE_DIR}/bootstrap.sh" --with-toolset=gcc
49+
WORKING_DIRECTORY ${BOOST_SOURCE_DIR})
50+
set(B2_EXECUTABLE "${BOOST_SOURCE_DIR}/b2")
51+
endif()
52+
53+
message(STATUS "Building Boost with b2 stage (this may take a long time)...")
54+
execute_process(
55+
COMMAND ${B2_EXECUTABLE} stage
56+
toolset=gcc
57+
address-model=64
58+
link=static
59+
runtime-link=static
60+
threading=multi
61+
--layout=versioned
62+
--build-type=complete
63+
WORKING_DIRECTORY ${BOOST_SOURCE_DIR}
64+
)
65+
66+
set(Boost_ROOT "${BOOST_SOURCE_DIR}")
67+
find_package(Boost 1.83 QUIET)
68+
else()
69+
message(STATUS "Boost found in the system.")
70+
endif()
71+
72+
# When using our staged Boost, FindBoost does not set Boost_LIBRARIES; collect built libs from stage.
73+
# b2 --build-type=complete produces both release (-mt-s-) and debug (-mt-sd-) variants; link only one
74+
# to avoid multiple definition errors.
75+
if(Boost_FOUND AND EXISTS "${BOOST_STAGE_LIB_DIR}")
76+
file(GLOB_RECURSE _stage_libs "${BOOST_STAGE_LIB_DIR}/*.a" "${BOOST_STAGE_LIB_DIR}/*.lib")
77+
if(_stage_libs)
78+
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
79+
list(FILTER _stage_libs INCLUDE REGEX "[-]mt[-]sd[-]|[-]sgd[-]")
80+
else()
81+
list(FILTER _stage_libs EXCLUDE REGEX "[-]mt[-]sd[-]|[-]sgd[-]")
82+
endif()
83+
set(Boost_LIBRARIES ${_stage_libs})
84+
endif()
85+
elseif(Boost_FOUND)
86+
set(Boost_LIBRARIES boost_filesystem boost_system boost_chrono boost_date_time boost_regex boost_program_options boost_serialization boost_thread boost_wave boost_iostreams boost_locale boost_unit_test_framework boost_wave boost_iostreams boost_locale boost_unit_test_framework)
87+
if(LINUX)
88+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L/usr/local/lib")
89+
endif()
90+
endif()
91+
# Do not pass NOTFOUND to the linker
92+
if(Boost_LIBRARIES)
93+
list(FILTER Boost_LIBRARIES EXCLUDE REGEX "NOTFOUND$")
94+
endif()
95+
96+
add_library(boost INTERFACE)
97+
target_include_directories(boost INTERFACE ${Boost_INCLUDE_DIRS})
98+
if(Boost_LIBRARIES)
99+
target_link_libraries(boost INTERFACE ${Boost_LIBRARIES})
100+
endif()

0 commit comments

Comments
 (0)