Skip to content

Commit 892fddd

Browse files
Merge pull request #24 from DUDUACM/cmake-support
CMake support
2 parents 7e543d3 + 948a0c7 commit 892fddd

7 files changed

Lines changed: 373 additions & 0 deletions

File tree

CMAKE_BUILD.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# CMake build guide
2+
3+
Support CMake build system
4+
5+
## Use CMake build
6+
7+
### Build
8+
9+
```bash
10+
# Create build dir
11+
mkdir build
12+
cd build
13+
14+
# Config project(Use Qt6)
15+
cmake ..
16+
17+
# Or assign Qt5
18+
cmake -DQT_VERSION_MAJOR=5 ..
19+
20+
# Build
21+
cmake --build .
22+
23+
# install (optional)
24+
cmake --install .
25+
```
26+
27+
### Build optional
28+
29+
- `ACSS_BUILD_STATIC`: Build static library (default is OFF, build shared library)
30+
- `ACSS_BUILD_EXAMPLES`: Build example programs (default is ON)
31+
32+
#### Build static library examples
33+
34+
```bash
35+
cmake -DACSS_BUILD_STATIC=ON ..
36+
cmake --build .
37+
```
38+
39+
#### Do not build example programs
40+
41+
```bash
42+
cmake -DACSS_BUILD_EXAMPLES=OFF ..
43+
cmake --build .
44+
```
45+
46+
### Specify Qt installation path
47+
48+
If CMake cannot find Qt automatically, you can specify it manually:
49+
50+
```bash
51+
cmake -DCMAKE_PREFIX_PATH="C:/Qt/6.5.0/msvc2019_64" ..
52+
```
53+
54+
### Specify the installation directory
55+
56+
```bash
57+
cmake -DCMAKE_INSTALL_PREFIX=/path/to/install ..
58+
cmake --build .
59+
cmake --install .
60+
```
61+
62+
## Use in other projects
63+
64+
### Use CMake
65+
66+
In your project's CMakeLists.txt:
67+
68+
```cmake
69+
find_package(QtAdvancedStylesheets REQUIRED)
70+
71+
add_executable(myapp main.cpp)
72+
73+
target_link_libraries(myapp
74+
PRIVATE
75+
QtAdvancedStylesheets::qtadvancedcss
76+
)
77+
```
78+
79+
### Use qmake
80+
81+
The original qmake build system remains unchanged and can be used as before.
82+
83+
## Project Structure
84+
85+
```tree
86+
87+
Qt-Advanced-Stylesheets/
88+
├── CMakeLists.txt # Main CMake file
89+
├── cmake/ # CMake configuration file
90+
│ └── QtAdvancedStylesheetsConfig.cmake.in
91+
├── src/ # source
92+
│ └── CMakeLists.txt
93+
├── examples/ # Example Programs
94+
│ ├── CMakeLists.txt
95+
│ ├── exporter/
96+
│ │ └── CMakeLists.txt
97+
│ └── full_features/
98+
│ └── CMakeLists.txt
99+
└── styles/ # style files
100+
```
101+
102+
## Precautions / Notes
103+
104+
1. Minimum CMake version required: 3.16
105+
2. Support Qt5 and Qt6
106+
3. By default, shared libraries are built. The Debug version will have a 'd' suffix added.
107+
4. Output files:
108+
- Library file: `build/lib/`
109+
- Executable file: `build/bin/`

CMakeLists.txt

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
3+
project(QtAdvancedStylesheets VERSION 1.0.5 LANGUAGES CXX)
4+
5+
# Set C++ standard
6+
set(CMAKE_CXX_STANDARD 14)
7+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
8+
9+
# Qt specific settings
10+
set(CMAKE_AUTOMOC ON)
11+
set(CMAKE_AUTORCC ON)
12+
set(CMAKE_AUTOUIC ON)
13+
14+
# Build type options
15+
option(ACSS_BUILD_STATIC "Build as static library" OFF)
16+
option(ACSS_BUILD_EXAMPLES "Build examples" ON)
17+
18+
# Set output directories
19+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
20+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
21+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
22+
23+
# Find Qt packages
24+
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
25+
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS
26+
Core
27+
Gui
28+
Widgets
29+
Qml
30+
Svg
31+
)
32+
33+
# Installation settings
34+
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
35+
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/installed" CACHE PATH "Install prefix" FORCE)
36+
endif()
37+
38+
# Add subdirectories
39+
add_subdirectory(src)
40+
41+
if(ACSS_BUILD_EXAMPLES)
42+
add_subdirectory(examples)
43+
endif()
44+
45+
# Installation
46+
install(EXPORT QtAdvancedStylesheetsTargets
47+
FILE QtAdvancedStylesheetsTargets.cmake
48+
NAMESPACE QtAdvancedStylesheets::
49+
DESTINATION lib/cmake/QtAdvancedStylesheets
50+
)
51+
52+
# Create config file
53+
include(CMakePackageConfigHelpers)
54+
write_basic_package_version_file(
55+
"${CMAKE_CURRENT_BINARY_DIR}/QtAdvancedStylesheetsConfigVersion.cmake"
56+
VERSION ${PROJECT_VERSION}
57+
COMPATIBILITY AnyNewerVersion
58+
)
59+
60+
configure_package_config_file(
61+
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/QtAdvancedStylesheetsConfig.cmake.in"
62+
"${CMAKE_CURRENT_BINARY_DIR}/QtAdvancedStylesheetsConfig.cmake"
63+
INSTALL_DESTINATION lib/cmake/QtAdvancedStylesheets
64+
)
65+
66+
install(FILES
67+
"${CMAKE_CURRENT_BINARY_DIR}/QtAdvancedStylesheetsConfig.cmake"
68+
"${CMAKE_CURRENT_BINARY_DIR}/QtAdvancedStylesheetsConfigVersion.cmake"
69+
DESTINATION lib/cmake/QtAdvancedStylesheets
70+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@PACKAGE_INIT@
2+
3+
include("${CMAKE_CURRENT_LIST_DIR}/QtAdvancedStylesheetsTargets.cmake")
4+
5+
check_required_components(QtAdvancedStylesheets)

examples/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Add example subdirectories
2+
add_subdirectory(exporter)
3+
add_subdirectory(full_features)

examples/exporter/CMakeLists.txt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
set(TARGET_NAME exporter)
2+
3+
# Find additional Qt components
4+
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Gui Widgets)
5+
6+
# Source files
7+
set(SOURCES
8+
exporter.cpp
9+
)
10+
11+
# Create executable
12+
add_executable(${TARGET_NAME} ${SOURCES})
13+
14+
# Link libraries
15+
target_link_libraries(${TARGET_NAME}
16+
PRIVATE
17+
QtAdvancedStylesheets::qtadvancedcss
18+
Qt${QT_VERSION_MAJOR}::Core
19+
Qt${QT_VERSION_MAJOR}::Gui
20+
Qt${QT_VERSION_MAJOR}::Widgets
21+
)
22+
23+
# Include directories
24+
target_include_directories(${TARGET_NAME}
25+
PRIVATE
26+
${CMAKE_SOURCE_DIR}/src
27+
)
28+
29+
# Compiler definitions
30+
target_compile_definitions(${TARGET_NAME}
31+
PRIVATE
32+
QT_DEPRECATED_WARNINGS
33+
STYLES_DIR=${CMAKE_SOURCE_DIR}/styles
34+
)
35+
36+
if(ACSS_BUILD_STATIC)
37+
target_compile_definitions(${TARGET_NAME} PRIVATE ACSS_STATIC)
38+
endif()
39+
40+
# Set target properties
41+
set_target_properties(${TARGET_NAME} PROPERTIES
42+
WIN32_EXECUTABLE FALSE
43+
MACOSX_BUNDLE FALSE
44+
)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
set(TARGET_NAME full_features)
2+
3+
# Find additional Qt components
4+
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS
5+
Core
6+
Gui
7+
Widgets
8+
QuickWidgets
9+
Qml
10+
)
11+
12+
# Source files
13+
set(SOURCES
14+
main.cpp
15+
mainwindow.cpp
16+
)
17+
18+
# Header files
19+
set(HEADERS
20+
mainwindow.h
21+
)
22+
23+
# UI files
24+
set(FORMS
25+
mainwindow.ui
26+
)
27+
28+
# Resource files
29+
set(RESOURCES
30+
full_features.qrc
31+
)
32+
33+
# Create executable
34+
add_executable(${TARGET_NAME}
35+
${SOURCES}
36+
${HEADERS}
37+
${FORMS}
38+
${RESOURCES}
39+
)
40+
41+
# Link libraries
42+
target_link_libraries(${TARGET_NAME}
43+
PRIVATE
44+
QtAdvancedStylesheets::qtadvancedcss
45+
Qt${QT_VERSION_MAJOR}::Core
46+
Qt${QT_VERSION_MAJOR}::Gui
47+
Qt${QT_VERSION_MAJOR}::Widgets
48+
Qt${QT_VERSION_MAJOR}::QuickWidgets
49+
Qt${QT_VERSION_MAJOR}::Qml
50+
)
51+
52+
# Include directories
53+
target_include_directories(${TARGET_NAME}
54+
PRIVATE
55+
${CMAKE_SOURCE_DIR}/src
56+
)
57+
58+
# Compiler definitions
59+
target_compile_definitions(${TARGET_NAME}
60+
PRIVATE
61+
QT_DEPRECATED_WARNINGS
62+
STYLES_DIR=${CMAKE_SOURCE_DIR}/styles
63+
)
64+
65+
# Set target properties
66+
set_target_properties(${TARGET_NAME} PROPERTIES
67+
WIN32_EXECUTABLE TRUE
68+
MACOSX_BUNDLE TRUE
69+
)

src/CMakeLists.txt

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
set(TARGET_NAME qtadvancedcss)
2+
3+
# Source files
4+
set(SOURCES
5+
QmlStyleUrlInterceptor.cpp
6+
QtAdvancedStylesheet.cpp
7+
)
8+
9+
# Header files
10+
set(HEADERS
11+
acss_globals.h
12+
QmlStyleUrlInterceptor.h
13+
QtAdvancedStylesheet.h
14+
)
15+
16+
# Create library
17+
if(ACSS_BUILD_STATIC)
18+
add_library(${TARGET_NAME} STATIC ${SOURCES} ${HEADERS})
19+
target_compile_definitions(${TARGET_NAME} PUBLIC ACSS_STATIC)
20+
else()
21+
add_library(${TARGET_NAME} SHARED ${SOURCES} ${HEADERS})
22+
target_compile_definitions(${TARGET_NAME} PRIVATE ACSS_SHARED_EXPORT)
23+
endif()
24+
25+
# Add alias for consistent usage
26+
add_library(QtAdvancedStylesheets::${TARGET_NAME} ALIAS ${TARGET_NAME})
27+
28+
# Set target properties
29+
set_target_properties(${TARGET_NAME} PROPERTIES
30+
VERSION ${PROJECT_VERSION}
31+
SOVERSION ${PROJECT_VERSION_MAJOR}
32+
OUTPUT_NAME qtadvancedcss
33+
DEBUG_POSTFIX d
34+
)
35+
36+
# Link Qt libraries
37+
target_link_libraries(${TARGET_NAME}
38+
PUBLIC
39+
Qt${QT_VERSION_MAJOR}::Core
40+
Qt${QT_VERSION_MAJOR}::Gui
41+
Qt${QT_VERSION_MAJOR}::Widgets
42+
Qt${QT_VERSION_MAJOR}::Qml
43+
Qt${QT_VERSION_MAJOR}::Svg
44+
)
45+
46+
# Include directories
47+
target_include_directories(${TARGET_NAME}
48+
PUBLIC
49+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
50+
$<INSTALL_INTERFACE:include>
51+
)
52+
53+
# Compiler warnings
54+
target_compile_definitions(${TARGET_NAME} PRIVATE QT_DEPRECATED_WARNINGS)
55+
56+
if(MSVC)
57+
target_compile_options(${TARGET_NAME} PRIVATE /utf-8)
58+
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
59+
target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra -pedantic)
60+
endif()
61+
62+
# Installation
63+
install(TARGETS ${TARGET_NAME}
64+
EXPORT QtAdvancedStylesheetsTargets
65+
LIBRARY DESTINATION lib
66+
ARCHIVE DESTINATION lib
67+
RUNTIME DESTINATION bin
68+
INCLUDES DESTINATION include
69+
)
70+
71+
install(FILES ${HEADERS}
72+
DESTINATION include
73+
)

0 commit comments

Comments
 (0)