[CMake] How to get list of generated object-files of OBJECT-library?

Deniz Bahadir dbahadir at benocs.com
Tue Jul 18 07:46:47 EDT 2017


Am 17.07.2017 um 18:41 schrieb Deniz Bahadir:
> Am 17.07.2017 um 17:15 schrieb Nils Gladitz:
>> On 17.07.2017 17:13, Deniz Bahadir wrote:
>>> Am 17.07.2017 um 16:47 schrieb Nils Gladitz:
>>>> On 7/17/2017 3:52 PM, Deniz Bahadir wrote:
>>>>>
>>>>> This works just fine. However, now I want to add an additional 
>>>>> build-step after creation of the object-files and before linking 
>>>>> the shared library. (In particular, I want to compress the 
>>>>> debug-symbols in the object-files. But that should be irrelevant 
>>>>> for my question.)
>>>>>
>>>>> My first attempt was to add the following between the two 
>>>>> add_library commands:
>>>>> ```
>>>>> add_custom_command( TARGET ${PROJECT_NAME}_OBJECTS POST_BUILD
>>>>>     COMMAND objcopy --compress-debug-sections 
>>>>> $<TARGET_OBJECTS:${PROJECT_NAME}_OBJECT>
>>>>> )
>>>>> ```
>>>>> But that results in the following error:
>>>>> ```
>>>>> (add_custom_command):
>>>>>   Target "MyProject_OBJECTS" is an OBJECT library that may not
>>>>>   have PRE_BUILD, PRE_LINK, or POST_BUILD commands.
>>>>> ```
>>>>>
>>>>> So I tried instead to add the following between the two add_library 
>>>>> commands:
>>>>> ```
>>>>> add_custom_command( TARGET ${PROJECT_NAME} PRE_LINK
>>>>>     COMMAND objcopy --compress-debug-sections 
>>>>> $<TARGET_OBJECTS:${PROJECT_NAME}_OBJECT>
>>>>> )
>>>>> ```
>>>>> This then fails when evaluating the generator-expression:
>>>>> ```
>>>>> (add_custom_command):
>>>>>   Error evaluating generator expression:
>>>>>
>>>>>     $<TARGET_OBJECTS:MyProject_OBJECTS>
>>>>>
>>>>>   The evaluation of the TARGET_OBJECTS generator expression is only 
>>>>> suitable
>>>>>   for consumption by CMake.  It is not suitable for writing out 
>>>>> elsewhere.
>>>>> ```
>>>>>
>>>>> So I am currently out of ideas (which do not make the 
>>>>> CMakeLists.txt file completely unmaintainable). Therefore my 
>>>>> question is:
>>>>>
>>>>> How can I retrieve the list of generated object-files so that I can 
>>>>> pass it to another program that should be run as additional 
>>>>> build-step before linking?
>>>>>
>>>>> IMHO, the most comfortable (and syntactically cleanest) way would 
>>>>> be my first attempt. But that is probably more a feature-request 
>>>>> than a simple question.
>>>>
>>>> FWIW this was implemented in the upcoming 3.9:
>>>> https://cmake.org/cmake/help/latest/module/CPackIFW.html#command:cpack_ifw_configure_component 
>>>>
>>>
>>> Thank you for your answer, Nils, but I do not really see how this 
>>> relates.
>>> This command seems to address CPack, while I am still struggling with 
>>> CMake building files (that later might be installed by CPack). 
>>
>> So sorry about that.
>> Must have gotten the wrong link in my clipboard.
>>
>> The correct link is:
>>      https://cmake.org/cmake/help/latest/release/3.9.html#other
> 
> Thanks a lot. That makes more sense now.
> So I will wait for CMake 3.9 to automagically fix my problem. :-)
> 

I just realized that CMake 3.9 does actually support my second attempt 
(with some modifications), not my first attempt.
The first one still fails with the same reason: An OBJECT-library does 
not support add_custom_command with POST_BUILD.

The working solution is to use the following command (which is a 
slightly modification of my second attempt from above):
```
add_custom_command( TARGET ${PROJECT_NAME} PRE_LINK
     COMMAND bash -c "for i in $(echo 
'$<TARGET_OBJECTS:${PROJECT_NAME}_OBJECTS>' | tr '[;]' ' '); do objcopy 
--compress-debug-sections $i; done"
     VERBATIM
)
```
(Note, that I had to somehow get rid of the semicolon which was used as 
delimiter in the list of target-objects because in bash a semicolon is 
seen as a command-delimiter.)

However, although that worked, it led to another problem:
objcopy is modifying the object-files so that at least ninja thinks they 
need to be rebuild the next time it is run. (Probably due to the fact, 
that the hashes changed. make might be fine because it just looks at the 
modification-date.)

If I would have been able to use my first attempt, that means 
associating the object-files manipulation with the OBJECT-library 
directly, that might not have been a problem, because the hash of the 
object-files would not change and only be taken into consideration after 
the OBJECT-library would have been created.
Is that observation correct!?

Are there any plans to support OBJECT-libraries with add_custom_command 
and PRE_BUILD/POST_BUILD commands?

Or does anyone have another idea how to prevent ninja from rebuilding 
all object-files after they were modified?

Thanks in advance,
Deniz

PS: Just letting objcopy modify copies of the object-files is no valid 
workaround as it defeats its purpose in the fist place, which is to 
reduce the space needed for the build-products on the harddrive.



More information about the CMake mailing list