cmake project version
-
Qt ver 5.15.13 for 32-bit compile and Qt 6.2.8 for 64-bit compile on Windows 10
I am using cmake to build a fairly large project in which I want to be able to compile using MinGW 64-bit vs MinGW 32-bit. I would like the cmake file to be fully automated, e.g. I do not want to have to edit it to tell the system I am building with 32-bit vs 64-bit. Here are relevant steps of my cmake file:
set(CMAKE_C_COMPILER gcc) cmake_minimum_required(VERSION 3.16) if (EXISTS ${CMAKE_CURRENT_LIST_DIR}/../BIPS10Ver.txt) file(READ "${CMAKE_CURRENT_LIST_DIR}/../BIPS10Ver.txt" FILE_VRSN) message("Read BIPS10Ver.txt ${FILE_VRSN}") else() message("Could not find BIPS10Ver.txt file") endif () project(BIPSMain VERSION ${FILE_VRSN} LANGUAGES CXX RC) # configure a header file to pass some of the CMake settings # to the source code configure_file(BIPS10Config.h.in BIPS10Config.h) # won't work before project()! if(CMAKE_SIZEOF_VOID_P EQUAL 8) set(IS64BIT 1) message("Using 64-Bit ") elseif(CMAKE_SIZEOF_VOID_P EQUAL 4) unset(IS64BIT) endif()
The first step here is to read a file that contains the software version.
I then use the cmake project command which includes the version just read.
Further down, I detect 32-bit vs 64-bit, which I am told does not work before the project command.
But now, if we are using 64-bit compiler, I want to read a different file to get version: BIPS10-64Ver.txt and set the project version with the new setting.
Is it possible to set the project version after the initial setting? -
Hi @nekkceb,
It seems that
CMAKE_SIZEOF_VOID_P
is only set after the CPP/CXX compiler is detected, which doesn't occur until afterproject()
tells cmake that you're using C++. From the docs:For the very first
project()
call only: ... Set the variables describing the host and target platforms.Note, there are some ways it can be triggered without
project()
being called yet, but that's because they implicitly callproject(Project)
. From the docs:The top-level
CMakeLists.txt
file for a project must contain a literal, direct call to theproject()
command; loading one through theinclude()
command is not sufficient. If no such call exists, CMake will issue a warning and pretend there is aproject(Project)
at the top to enable the default languages (C and CXX).So, if you really need to vary the project version based on pointer size, you can probably either set the various
PROJECT_*_VERSION
variables yourself, or, simply re-issue theproject()
command. Consider, this example:message(STATUS "Project: ${PROJECT_NAME} ${PROJECT_VERSION}") message(STATUS "Size of void*: '${CMAKE_SIZEOF_VOID_P}'") message(STATUS "---") project(example VERSION 1.2.3 LANGUAGES CXX) message(STATUS "Project: ${PROJECT_NAME} ${PROJECT_VERSION}") message(STATUS "Size of void*: '${CMAKE_SIZEOF_VOID_P}'") message(STATUS "---") project(example VERSION 3.4.5 LANGUAGES CXX) message(STATUS "Project: ${PROJECT_NAME} ${PROJECT_VERSION}") message(STATUS "Size of void*: '${CMAKE_SIZEOF_VOID_P}'")
Output:
-- CMake version is 3.24.2 -- Project: -- Size of void*: '' -- --- -- Project: example 1.2.3 -- Size of void*: '8' -- --- -- Project: example 3.4.5 -- Size of void*: '8'
That said, I wonder why you want to do this? Can you tell use what's different in the two version files? From the docs, the project version is
composed of non-negative integer components, i.e.
<major>[.<minor>[.<patch>[.<tweak>]]]
And modern cmake's will error if this format is not adhered to (older cmake's were more lenient, though presumably you can set some policy to relax it, it's still good to avoid deprecated functionality). And it seems unusual that you'd want to include 32/64-bit build information there. Instead, you might (depending on your use case), be better introducing your own version suffix / build-info variable, which is pretty easy to do and can be easily combined into the definitions your C++ code received, without any (C++) code changes needed (just some small tweaks to your cmake file/s). If you can show us the differences in your two versions, we might be able to suggest alternatives :)
Cheers.
-
@nekkceb said in cmake project version:
Is it possible to set the project version after the initial setting?
Yes please see https://cmake.org/cmake/help/latest/variable/PROJECT_VERSION.html