5

I'm adding a component built with CMake into a larger project which currently uses makefiles. There is a single "configuration" makefile, which contains some logic which, among others, sets up target directories in the form of make variables. The config makefiles is included in the current build by setting the MAKEFILES env var, and the actual build makefiles reference these varaibles when building and linking to artifacts in these directories, so e.g.:

# in config.mk
...
MY_TARGET_LIB_DIR=...

# in a build makefile
TGT_LIB=$(MY_TARGET_LIB_DIR)/libsomething.so

# in another build makefile
LDFLAGS=-L$(MY_TARGET_LIB_DIR)

What I would like to do is make use of this in the makefiles generated by CMake, i.e. have CMake generate me a makefile that builds a library (static in this case) in the directory referenced by the variable MY_TARGET_LIB_DIR and another that links to a library in that directory.

I have tried to do something like

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY $(MY_TARGET_LIB_DIR))

but no luck - this only causes the generated makefile to output into a subdirectory literally called $(MY_TARGET_LIB_DIR). Is there a solution for this?

2 Answers 2

5

That's pretty easy to do via ${ENV[...]}, since Make auto-exports all make variables into the environment.

An example:

[ttt] cat CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(ttt)

add_custom_target(t
    COMMAND echo `printenv ZZZZ`...
    #VERBATIM
    )

So then, we can vary ZZZZ after the generation step, at make time:

[ttt] cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/ttt
[ttt]
[ttt]
[ttt] make ZZZZ=foobar t
foobar...
Built target t
[ttt] ZZZZ=buzzquux make t
buzzquux...
Built target t
[ttt]
[ttt]
[ttt] export ZZZZ=bbb
[ttt] make t
bbb...
Built target t

It's enlightening to inspect the generated makefiles (esp. when anything goes wrong):

[ttt] grep -C2 ZZZZ CMakeFiles/t.dir/build.make

CMakeFiles/t:
    echo `printenv ZZZZ`...

t : CMakeFiles/t
Sign up to request clarification or add additional context in comments.

Comments

0

No, you cannot tell CMake generated Makefiles to use variables from other, non-CMake Makefiles. Nor can they parse passed arguments from the shell.

You can pass variables during configure by cmake -D<variable>=<value> that's all. But usually you don't want to configure during a make run.

4 Comments

You have seem to misunderstood my question - I am not trying to parse any arguments passed from the shell. What I am trying to do is use a varaible defined in an existing makefile, possibly by having a literal, non-escaped $(variable_name) string in the makefile generated by CMake. Passing the value through -D might be a solution, but how would I get the value of the variable? Would I have to run CMake from a makefile to generate another makefile?
I got your question, my answer was just too short. With -D you can just use the variable insight your CMake files. But in general you don't want to run cmake (which is configure) for every build run.
I know about -D, but I need the varaible's value in order to set it, right? The problem is I'm running CMake from shell and I don't have the value from there - it's a makefile variable, so (unless I'm missing something), its value is only available when running make (by including config.mk from my example).
Yeah, that's why I set you don't want to use -D. I don't see a way to achieve what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.