[CMake] how to set ar/ranlib flags per target - Solved

Hickel, Kelly Kelly_Hickel at bmc.com
Thu Jul 22 14:35:16 EDT 2010


It turns out that you can do this (yay!), it's just tricky to discover how (well, it was for ME!).

I got to wondering when CMake was injecting the STATIC_LIBRARY_FLAGS into the ar command line. The answer to this is also the reason why my setting LINK_FLAGS didn't have any effect on the ar command line.  Apparently, cmMakefileLibraryTargetGenerator.cxx uses the target property STATIC_LIBRARY_FLAGS value and inserts it where <LINK_FLAGS> appears in the template.

Since STATIC_LIBRARY_FLAGS is per target, this does what I want, as long as I accompany it with :
  SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> <LINK_FLAGS> cr <TARGET> <OBJECTS>")
  SET(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> <LINK_FLAGS> r <TARGET> <OBJECTS>")
  SET(CMAKE_CXX_ARCHIVE_CREATE "<CMAKE_AR> <LINK_FLAGS> cr <TARGET> <OBJECTS>")
  SET(CMAKE_CXX_ARCHIVE_APPEND "<CMAKE_AR> <LINK_FLAGS> r <TARGET> <OBJECTS>")

The next problem was that adding those lines into my customized toolchain file didn't seem to work, something must be coming along and overriding those settings after my file is executed.  For me the simplest answer was to add those SET lines (with PARENT_SCOPE added to the line) into a function that I end up calling fairly frequently, to make sure I get the global setting that I want.  This is overkill, but it works just fine....


Anyway, just wanted to make sure the "solution" (well, workaround) made it into the mailing list archives.



Kelly Hickel







> -----Original Message-----
> From: cmake-bounces at cmake.org [mailto:cmake-bounces at cmake.org] On
> Behalf Of Hickel, Kelly
> Sent: Thursday, July 22, 2010 10:35 AM
> To: Verweij, Arjen; cmake at cmake.org
> Subject: Re: [CMake] how to set ar/ranlib flags per target
> 
> 
> > -----Original Message-----
> > From: cmake-bounces at cmake.org [mailto:cmake-bounces at cmake.org] On
> > Behalf Of Verweij, Arjen
> > Sent: Thursday, July 22, 2010 6:59 AM
> > To: cmake at cmake.org
> > Subject: Re: [CMake] how to set ar/ranlib flags per target
> >
> > Hi Kelly,
> >
> > I'm using cmake to migrate from a legacy build system. The source
> > consists of C C++ Fortran and Fortran90. I think -in all- there are
> 15
> > binaries.
> >
> > One of the problems for us is linking. Cmake somehow decides to take
> > compiler X to link executable Y. But in some cases it didn't use the
> > one we expected.
> >
> > So we wrapped add_executable() inside a function that takes an extra
> > argument to control the linker language:
> >
> > function (FEXECUTABLE EXE_NAME LINK_LANG )
> >
> > [...]
> >
> >     add_executable (${EXE_NAME_EXE} ${FILE_LIST_M})
> >
> >     if ( ${LINK_LANG} STREQUAL "CXX" )
> >         set_target_properties (${EXE_NAME_EXE} PROPERTIES
> > LINKER_LANGUAGE CXX)
> >     elseif ( ${LINK_LANG} STREQUAL "C" )
> >         set_target_properties (${EXE_NAME_EXE} PROPERTIES
> > LINKER_LANGUAGE C)
> >     elseif ( ${LINK_LANG} STREQUAL "Fortran" )
> >         set_target_properties (${EXE_NAME_EXE} PROPERTIES
> > LINKER_LANGUAGE Fortran)
> >     else ()
> >         message( FATAL "Unknown linker language specified for
> function
> > FEXECUTABLE: ${LINK_LANG" )
> >     endif ()
> >
> > [...]
> >
> > endfunction (FEXECUTABLE)
> >
> > I'm not saying that this is proper usage of cmake, but it "works for
> me
> > (tm)".
> >
> > Perhaps you can try a similar approach with add_library() -
> > myadd_static_library() ? - to indicate if you want to use a 32-bit or
> > 64-bit archiver.
> >
> > Regards,
> > Arjen
> 
> Thanks Arjen,  I just don't see how this can help.  This snippet is
> ensure that the desired LINKER_LANGUAGE is set for each target, but my
> immediate issue is that it doesn't seem possible to inject -X64 onto
> the ar command line, in the correct place at all, much less on a per
> target basis, at least not without code changes (which I may undertake,
> but I'd REALLY like to avoid that).
> 
> To sum up, I have these issues:
> 	1) The definition of CMAKE_C_ARCHIVE_CREATE indicates that it
> should use the LINK_FLAGS, but it doesn't do so and I don't know why.
> 	2) STATIC_LIBRARY_FLAGS *ALMOST* works, but it's putting the flag
> after the "/usr/bin/ar cr libfoo.a" and for AIX, it must come before
> the "cr".
> 	3) trying to manipulate things by setting CMAKE_AR for instance
> has a global effect.
> 
> I suppose my best course is to figure out a way to change the source to
> be able to be more selective on where STATIC_LIBRARY_FLAGS gets
> injected, but that seems kind of a large hurdle for me at the moment (I
> haven't looked at the source that much).
> 
> Thanks,
> -Kelly
> 
> 
> >
> > >-----Original Message-----
> > >From: Hickel, Kelly [mailto:Kelly_Hickel at bmc.com]
> > >Sent: donderdag 22 juli 2010 13:46
> > >To: Hickel, Kelly; Verweij, Arjen; cmake at cmake.org
> > >Subject: RE: how to set ar/ranlib flags per target
> > >
> > >Arjen, I saw your response about "wrapping add_library" on the list,
> > but
> > >for some reason it didn't get to my inbox.
> > >
> > >I'm not sure what you mean by "wrapping", are you talking about a
> code
> > >change, or some sort of macro?  So far, I haven't been able to come
> up
> > >with any way of jamming -X64 into the right place in the ar command.
> > >
> > >Thanks,
> > >
> > >
> > >Kelly Hickel
> > >
> > >
> > >
> > >> -----Original Message-----
> > >> From: cmake-bounces at cmake.org [mailto:cmake-bounces at cmake.org] On
> > >> Behalf Of Hickel, Kelly
> > >> Sent: Wednesday, July 21, 2010 3:46 PM
> > >> To: Verweij, Arjen; cmake at cmake.org
> > >> Subject: Re: [CMake] how to set ar/ranlib flags per target
> > >>
> > >>
> > >> > -----Original Message-----
> > >> > From: cmake-bounces at cmake.org [mailto:cmake-bounces at cmake.org]
> On
> > >> > Behalf Of Verweij, Arjen
> > >> > Sent: Wednesday, July 21, 2010 3:43 PM
> > >> > To: cmake at cmake.org
> > >> > Subject: Re: [CMake] how to set ar/ranlib flags per target
> > >> >
> > >> > Kelly,
> > >> >
> > >> > I suffer from the same problem and solved it like this:
> > >> >
> > >> > SET(CMAKE_C_ARCHIVE_CREATE "<CMAKE_AR> -X64 cr <TARGET>
> > <LINK_FLAGS>
> > >> > <OBJECTS>")
> > >> > SET(CMAKE_C_ARCHIVE_APPEND "<CMAKE_AR> -X64 r  <TARGET>
> > <LINK_FLAGS>
> > >> > <OBJECTS>")
> > >> > SET(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -X64 <TARGET>")
> > >> > SET(CMAKE_CXX_ARCHIVE_CREATE ${CMAKE_C_ARCHIVE_CREATE})
> > >> > SET(CMAKE_CXX_ARCHIVE_APPEND ${CMAKE_C_ARCHIVE_APPEND})
> > >> > SET(CMAKE_CXX_ARCHIVE_FINISH ${CMAKE_C_ARCHIVE_FINISH})
> > >> > SET(CMAKE_Fortran_ARCHIVE_CREATE ${CMAKE_C_ARCHIVE_CREATE})
> > >> > SET(CMAKE_Fortran_ARCHIVE_APPEND ${CMAKE_C_ARCHIVE_APPEND})
> > >> > SET(CMAKE_Fortran_ARCHIVE_FINISH ${CMAKE_C_ARCHIVE_FINISH})
> > >> >
> > >> > Does that help you?
> > >> >
> > >> > Regards,
> > >> > Arjen
> > >>
> > >> Thanks Arjen, but I don't believe it does.
> > >> As far as I know that sets the flag globally, I have some targets
> > that
> > >> are 32 bit and some that are 64 bit, so I need to set it per
> target.
> > >>
> > >> Reading the source, I found STATIC_LIBRARY_FLAGS, which *almost*
> > >works,
> > >> but it puts the flags AFTER the name of the output library, and ar
> > on
> > >> AIX doesn't like that, complains it can't find the object file -
> X64.
> > >> Apparently the flag must come before the "cr" or "r" commands,
> e.g.
> > >"ar
> > >> -X64 cr foo.a bar.o baz.o".
> > >>
> > >> Thanks,
> > >> -Kelly
> > >>
> > >> >
> > >> > >-----Original Message-----
> > >> > >From: cmake-bounces at cmake.org [mailto:cmake-bounces at cmake.org]
> On
> > >> > Behalf
> > >> > >Of Hickel, Kelly
> > >> > >Sent: woensdag 21 juli 2010 21:00
> > >> > >To: cmake at cmake.org
> > >> > >Subject: [CMake] how to set ar/ranlib flags per target
> > >> > >
> > >> > >
> > >> > >Hello,
> > >> > >
> > >> > >I'm using CMake 2.8.1, and have a problem on AIX similar to
> this
> > >> one:
> > >> > >http://web.archiveorange.com/archive/v/5y7PkUbT6iizO31eshQa .
> > >> > >
> > >> > >I have the additional complication of needing to build both 32
> > and
> > >> 64
> > >> > >bit libraries from the same set of CMake files.
> > >> > >
> > >> > >I've tried a number of things (list below), does anyone have
> any
> > >> > ideas?
> > >> > >
> > >> > >(Wherever I write CMAKE_C_xyz below, I've also changed
> > >CMAKE_CXX_xyz
> > >> > >at the same time, where I write xyz_ARCHIVE_CREATE, I've also
> > >> changed
> > >> > >xyz_ARCHIVE_APPEND)
> > >> > >
> > >> > >1) Adding the flag to LINK_FLAGS because the definition for
> > >> > >   CMAKE_C_ARCHIVE_CREATE appears to include that on the
> command
> > >> line,
> > >> > >   but the generated link.txt input files don't include any
> > >options.
> > >> > >2) Changed the definition for CMAKE_C_ARCHIVE_CREATE in a
> private
> > >> > >   toolchain file that I specify on the command line.  By
> writing
> > >> > >   messages that show the value of that variable, I can see my
> > >> change
> > >> > >   take, but it appears to get reset to the default before
> > >> processing
> > >> > of
> > >> > >   my CMakeLists.txt file begins.
> > >> > >3) Frequently set CMAKE_C_ARCHIVE_CREATE to "<CMAKE_AR>
> > <AR_FLAGS>
> > >r
> > >> > >   <TARGET> <LINK_FLAGS> <OBJECTS>", when I did this, I ended
> up
> > >> with
> > >> > >the
> > >> > >   literal string "AR_FLAGS" in the link.txt file, not that
> > useful!
> > >> > >
> > >> > >I'm sure I'm missing something, any hints will be appreciated!
> > >> > >
> > >> > >Thanks,
> > >> > >Kelly
> > >> > >
> > >> > >_______________________________________________
> > >> > >Powered by www.kitware.com
> > >> > >
> > >> > >Visit other Kitware open-source projects at
> > >> > >http://www.kitware.com/opensource/opensource.html
> > >> > >
> > >> > >Please keep messages on-topic and check the CMake FAQ at:
> > >> > >http://www.cmake.org/Wiki/CMake_FAQ
> > >> > >
> > >> > >Follow this link to subscribe/unsubscribe:
> > >> > >http://www.cmake.org/mailman/listinfo/cmake
> > >> > _______________________________________________
> > >> > Powered by www.kitware.com
> > >> >
> > >> > Visit other Kitware open-source projects at
> > >> > http://www.kitware.com/opensource/opensource.html
> > >> >
> > >> > Please keep messages on-topic and check the CMake FAQ at:
> > >> > http://www.cmake.org/Wiki/CMake_FAQ
> > >> >
> > >> > Follow this link to subscribe/unsubscribe:
> > >> > http://www.cmake.org/mailman/listinfo/cmake
> > >>
> > >> _______________________________________________
> > >> Powered by www.kitware.com
> > >>
> > >> Visit other Kitware open-source projects at
> > >> http://www.kitware.com/opensource/opensource.html
> > >>
> > >> Please keep messages on-topic and check the CMake FAQ at:
> > >> http://www.cmake.org/Wiki/CMake_FAQ
> > >>
> > >> Follow this link to subscribe/unsubscribe:
> > >> http://www.cmake.org/mailman/listinfo/cmake
> >
> > _______________________________________________
> > Powered by www.kitware.com
> >
> > Visit other Kitware open-source projects at
> > http://www.kitware.com/opensource/opensource.html
> >
> > Please keep messages on-topic and check the CMake FAQ at:
> > http://www.cmake.org/Wiki/CMake_FAQ
> >
> > Follow this link to subscribe/unsubscribe:
> > http://www.cmake.org/mailman/listinfo/cmake
> 
> _______________________________________________
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
> 
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
> 
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake



More information about the CMake mailing list