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

Michael Hertling mhertling at online.de
Wed Jul 21 22:55:43 EDT 2010


On 07/21/2010 10:46 PM, Hickel, Kelly wrote:
> 
>> -----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.

AFAIK, these rule variables are effective for the directory they are
defined in, and they are inherited as usual; look at the following:

# CMakeLists.txt:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(RULEVARS C)
SET(CMAKE_C_ARCHIVE_CREATE "echo \"Top directory\"; <CMAKE_AR> cr
<TARGET> <LINK_FLAGS> <OBJECTS>")
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/f.c "void f(void){}\n")
ADD_LIBRARY(f STATIC f.c)
ADD_SUBDIRECTORY(subdir1)
ADD_SUBDIRECTORY(subdir2)

# subdir1/CMakeLists.txt:
SET(CMAKE_C_ARCHIVE_CREATE "echo \"Sub directory\"; <CMAKE_AR> cr
<TARGET> <LINK_FLAGS> <OBJECTS>")
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/g.c "void g(void){}\n")
ADD_LIBRARY(g STATIC g.c)

# subdir2/CMakeLists.txt:
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/h.c "void h(void){}\n")
ADD_LIBRARY(h STATIC h.c)

After configuration and "make VERBOSE=1", you will see that the targets
"f" and "g" are built with their individual CMAKE_C_ARCHIVE_CREATE rule
while "h" inherits this rule from the parent directory. Thus, if it's
feasible to organize your project so that the 32- and 64-bit targets
reside in their own directories you could possibly get by with the
manipulation of the rule variables on a per-directory basis.

Regards,

Michael

P.S.: The latest definition of the rules in a directory takes effect
      for that entire directory, but the subdirectories inherit the
      definition which is valid at the ADD_SUBDIRECTORY(). Prove it
      by moving the SET() in the toplevel CMakeLists.txt to the end:
      "f" and "g" won't change, but "h" is built with the usual rule.

> 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


More information about the CMake mailing list