CMAKE_STRIP arguments?

Is there a way to pass arguments to cmake strip (invoked via ninja install/strip)? Is appending them to CMAKE_STRIP the correct approach? Can’t really find the docs to cmake stripping.

I suspect this is not documented since it is mostly internal. A feature request to document the semantics and whether appending works might be the way to go.

Thanks!

When I set CMAKE_STRIP with arguments it appears as the binary no longer gets stripped at all. The cmake_install.cmake file in the build folder looks correct, but the actual stripping does not take place. Can I see verbose output of cmake strip? I’m invoking it via ninja install/strip.

You can run the install script yourself:

$ cmake --trace-expand -DCMAKE_INSTALL_DO_STRIP=1 -P cmake_install.cmake

Thanks, for tip. But I still don’t understand why the file doesn’t become stripped when I add arguments to the strip command. Anything obvious wrong here? If I remove the trailing --strip-all from my CMAKE_STRIP then the file gets stripped.

-- Installing: /data/src/build/install/usr/bin/my_binary
/data/src/build/cmake_install.cmake(48):  if(EXISTS /data/src/build/install/usr/bin/my_binary AND NOT IS_SYMLINK /data/src/build/install/usr/bin/my_binary )
-- Set runtime path of "/data/src/build/install/usr/bin/my_binary" to ""
/data/src/build/cmake_install.cmake(54):  if(CMAKE_INSTALL_DO_STRIP )
/data/src/build/cmake_install.cmake(55):  execute_process(COMMAND strip --strip-all /data/src/build/install/usr/bin/my_binary )


How did you add --strip-all? If you set it to strip --strip-all, it is failing because that is not a valid command. You need to use strip;--strip-all to have CMake treat it as two items.

In toolchain file I set it like this

set(CMAKE_STRIP "strip; --strip-all")

That space means you’re passing a <space>--strip-all option and is probably treated as a filename, not a flag.

No that does not help. As soon as I pass an argument to CMAKE_STRIP that breaks stripping. I don’t see an error message, but the file does not get stripped. I’m invoking this via the install/strip target.

At this point, I would use strace to see what is actually happening and where things might be going wrong.

I think I can see the problem. Looks like cmake is passing the strip command also on the argument list to execve.

[pid 32262] execve("/usr/bin/strip;--strip-all", ["/usr/bin/strip;--strip-all", "/data/src/build/my_binary"], 0x7ffc6199e120 /* 62 vars */) = -1 ENOENT (No such file or directory)

That means that ${CMAKE_STRIP} is quoted somewhere and doesn’t support expanding to arguments. Finding and fixing that would let it work in newer CMake versions that contain the fix at least.

Which cmake has this fix? I’m running cmake version 3.16.5.

I don’t know that any CMake version has the fix yet.

Looking, the value is quoted in the written script in both usages:

  • cmInstallRuntimeDependencySetGenerator::GenerateStripFixup
  • cmInstallTargetGenerator::AddStripRule

So no arguments are supported even as of 3.25.

I see, thanks for checking. Can I instead use objcopy to circumvent the bug? E.g. objcopy --strip-debug via custom target on all files that are installed? Is there an easy way to traverse all install targets?

I also would like cmake to add gnu debug links, objcopy --add-gnu-debuglink, on the files installed and the link point to where the unstripped file with symbols resides.

I don’t know of an easy way to know where all targets will be installed at configure time to be able to set something like that up, sorry.

You can add install(CODE) commands to do that.

You can do it, but it needs to be done per-target with knowledge of the install destination manually transferred.

In an older mailing list thread was the suggestion to generate a shell script that calls strip with the desired arguments and assigning it to CMAKE_STRIP.

See https://cmake.org/pipermail/cmake/2011-April/043705.html

2 Likes