Allow configuring installation path of object libraries
The installation path of object library targets currently is not very configurable.
For the following project
cmake_minimum_required(VERSION 3.16)
project(proj)
set(obj_path ".some_hidden_folder/my_static_initializer.cpp")
file(WRITE "${obj_path}" "void foo() {}")
add_library(my_long_object_library_target_name OBJECT "${obj_path}")
install(TARGETS my_long_object_library_target_name DESTINATION lib)
configured with
cmake .. -DCMAKE_INSTALL_PREFIX=$PWD/installed -DCMAKE_CONFIGURATION_TYPES=Release -G 'Ninja Multi-Config'
the installed file path is
installed/lib/objects-Release/my_long_object_library_target_name/.some_hidden_folder/my_static_initializer.cpp.o
This leads to quite a long file path, which can cause issues on Windows due to path length restrictions.
There are a few things a project could tweak: like the target name and the cpp file name / source file path. But in some cases due to project restrictions it is difficult to do: target names are public API and can't be changed, same for source file names.
the objects-Release
can be considered entirely hardcoded (aside from using a differently named configuration, which is not feasible):
std::string computeInstallObjectDir(cmGeneratorTarget* gt,
std::string const& config)
{
std::string objectDir = "objects";
if (!config.empty()) {
objectDir += "-";
objectDir += config;
}
objectDir += "/";
objectDir += gt->GetName();
return objectDir;
}
Simply renaming the installed file and moving it into a different folder is also not feasible, because install(EXPORT)
hardcodes the paths where to look up the object files.
It would be great if the installation path could be tweaked regardless of target name and source file name and location.
One part could likely be achieved by making the OUTPUT_NAME
(source file?) property work for the object files.
And we'd need something additional:
- to configure the
objects-Release/
part - the target name part
my_long_object_library_target_name
- a way to avoid including the object file parent dir
.some_hidden_folder
Ideally this would also apply to build time file paths as well.
Assuming that this feature is ok, what do you think would be good knobs to configure the last 3 parts?
Would an OBJECT_FILE_OUTPUT_PATH
source file property make sense, with a value that looks like
objs-$<CONFIG>/$<TARGET_FILE_NAME>/$<TARGET_OBJECT_FILE_NAME>.$<TARGET_FILE_SUFFIX>
?
Where $<TARGET_OBJECT_FILE_NAME>
would be a new generator expression.