[CMake] How to use a generated linker map with a shared library?

Paul Smith paul at mad-scientist.net
Sat Jan 14 18:46:23 EST 2017


I'm really stuck: maybe someone can provide a hint.

I'm trying to create a shared library from a bunch of object files, and
use a linker script to mark almost all the symbols as local.  The tricky
thing is I'm auto-generating the linker script via a shell script that
examines the object files for my library (using readelf) to find global
symbols.

I basically need a process like this:

   1. Start with source files and a linker map generator shell script
   2. Compile all the code into object files
   3. If any file was recompiled (or the generator script) is modified, re-
      run the generator script to recreate the linker script
   4. If any file was recompiled or the linker script was recreated,
      rebuild the shared library, using the linker script.

In "make-ese" it would be trivial: something like:

  libmy.so: $(OBJECTS) link.script ; <link .so> ...

  link.script: $(OBJECTS) genlinkscript ; <generate script> ...

I simply can't figure out how to create something like this in CMake.

I have a custom command to create the linker script:

  add_custom_command(OUTPUT out.script
      COMMAND genlinkscript ${CMAKE_CURRENT_BINARY_DIR}
      DEPENDS <<SOMETHING GOES HERE>>
      VERBATIM)

And I have an add_library command to create the shared library:

  add_library(mylib SHARED foo.cpp foo.h out.script)

  set_property(TARGET mylib APPEND_STRING PROPERTY
        LINK_FLAGS " -Wl,--version-script=out.script")

But what can my custom_command depend on, so that it's run AFTER all the
object files are created, but BEFORE the shared library is created?

I tried to do this using an OBJECT library, but see my previous post
that adding OBJECT libraries as dependencies of custom commands doesn't
work.

What can I do?  I'm happy with a solution that works only on Linux (I
can use if()/endif() to turn it off on my other platforms, which don't
use these linker scripts anyway).


More information about the CMake mailing list