Skip to content

Commit e4fbeac

Browse files
committed
Add preferred Qt install path and version configuration in CMakePresets.json. Also allows overriding Qt version per build type.
1 parent 31e1a93 commit e4fbeac

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

SerialPrograms/CMakeLists.txt

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,31 +110,47 @@ if(NOT QT_MAJOR)
110110
set(QT_MAJOR 6)
111111
endif()
112112

113-
find_package(Qt${QT_MAJOR} COMPONENTS Widgets SerialPort Multimedia MultimediaWidgets REQUIRED)
113+
# Make sure CI builds work without going overboard with per-platform directory shenanigans
114+
if(NOT DEPLOY_FILES)
115+
find_package(Qt${QT_MAJOR} COMPONENTS Widgets SerialPort Multimedia MultimediaWidgets REQUIRED)
116+
endif()
114117

115118
# Find Qt and windeployqt.exe relative to the Qt bin dir
116119
if(WIN32 AND DEPLOY_FILES)
117120
# Define the base Qt installation directory
118-
set(QT_BASE_DIR "C:/Qt")
121+
if(PREFERRED_QT_DIR AND PREFERRED_QT_VER)
122+
set(QT_CANDIDATE_DIR "${PREFERRED_QT_DIR}/${PREFERRED_QT_VER}/msvc2022_64/lib/cmake")
123+
endif()
119124

120-
# Find all subdirectories in the Qt base directory
121-
file(GLOB QT_SUBDIRS LIST_DIRECTORIES true "${QT_BASE_DIR}/${QT_MAJOR}*")
125+
# Set default base directory just in case, validate that Qt specified in CMakePresets.json exists
126+
# Otherwise search out the latest available in the default Qt directory.
127+
set(QT_BASE_DIR "C:/Qt")
128+
if(QT_CANDIDATE_DIR AND EXISTS "${QT_CANDIDATE_DIR}")
129+
message(STATUS "Using preferred Qt directory for Qt${QT_MAJOR} ${PREFERRED_QT_VER}: ${QT_CANDIDATE_DIR}")
130+
set(CMAKE_PREFIX_PATH "${QT_CANDIDATE_DIR}")
131+
find_package(Qt${QT_MAJOR} ${PREFERRED_QT_VER} COMPONENTS Widgets SerialPort Multimedia MultimediaWidgets REQUIRED)
132+
else()
133+
# Find all subdirectories in the Qt base directory
134+
find_package(Qt${QT_MAJOR} COMPONENTS Widgets SerialPort Multimedia MultimediaWidgets REQUIRED)
135+
file(GLOB QT_SUBDIRS LIST_DIRECTORIES true "${QT_BASE_DIR}/${QT_MAJOR}*")
122136

123-
# Filter and sort the directories to find the latest version
124-
list(SORT QT_SUBDIRS)
125-
list(REVERSE QT_SUBDIRS)
137+
# Filter and sort the directories to find the latest version
138+
list(SORT QT_SUBDIRS)
139+
list(REVERSE QT_SUBDIRS)
126140

127-
# Get the first directory in the sorted list (latest version)
128-
list(GET QT_SUBDIRS 0 QT_LATEST_DIR)
141+
# Get the first directory in the sorted list (latest version)
142+
list(GET QT_SUBDIRS 0 QT_LATEST_DIR)
129143

130-
# Debug message to verify the latest Qt directory
131-
if(QT_LATEST_DIR)
132-
message(STATUS "Latest Qt directory for Qt${QT_MAJOR}: ${QT_LATEST_DIR}")
133-
set(CMAKE_PREFIX_PATH "${QT_LATEST_DIR}/msvc2022_64/lib/cmake")
134-
else()
135-
message(FATAL_ERROR "No Qt${QT_MAJOR} installation found in ${QT_BASE_DIR}")
144+
# Debug message to verify the latest Qt directory
145+
if(QT_LATEST_DIR)
146+
message(STATUS "Latest Qt directory for Qt${QT_MAJOR}: ${QT_LATEST_DIR}")
147+
set(CMAKE_PREFIX_PATH "${QT_LATEST_DIR}/msvc2022_64/lib/cmake")
148+
else()
149+
message(FATAL_ERROR "No Qt${QT_MAJOR} installation found in ${QT_BASE_DIR}")
150+
endif()
136151
endif()
137152

153+
# Find windeployqt.exe for Qt dependency deployment
138154
get_target_property(qt_core_location Qt${QT_MAJOR}::Core LOCATION)
139155
get_filename_component(qt_bin_dir "${qt_core_location}" DIRECTORY)
140156
find_program(WINDEPLOYQT_EXECUTABLE windeployqt

SerialPrograms/CMakePresets.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
"CMAKE_AUTOMOC": "ON",
1919
"CMAKE_AUTORCC": "ON",
2020
"CMAKE_AUTOUIC": "ON",
21-
"PACKAGE_BUILD": "OFF"
21+
"PACKAGE_BUILD": "OFF",
22+
"PREFERRED_QT_DIR": "C:/Qt",
23+
"PREFERRED_QT_VER": "6.8.3",
2224
}
2325
},
2426
{
@@ -99,4 +101,4 @@
99101
"configurePreset": "Publish"
100102
}
101103
]
102-
}
104+
}

0 commit comments

Comments
 (0)