How can I run two makefile targets such that the second uses the first's variables?
I am writing a makefile that has multiple targets for architectures and build types. When I call make I would like to be able to pass it two targets such as:
make x86 debug
Where the architecture target sets variables to be used by the build target. An architecture target might look like this:
.PHONY: x86
x86: NAME_PREFIX = x86_64_Linux
x86: TARGET_TRIPLE = x86_64-pc-linux-elf
x86: CC = gcc
x86: CFLAGS += -fomit-frame-pointer
First issue: If I run the two targets, make skips the first target since it does nothing but set variables.
$ make x86 debug
make: Nothing to be done for `x86'.
Second issue: Even if it would run the first target, my understanding is that the values won't be retained when make starts running the second target anyway.
For now, I'm using targets that set variables and add the build type target as a prerequisite at the end of the target body, but I would end up needing a target for every combination of architecture and build type.
.PHONY: x86_debug
x86_debug: NAME_PREFIX = x86_64_Linux
x86_debug: TARGET_TRIPLE = x86_64-pc-linux-elf
x86_debug: CC = gcc
x86_debug: CFLAGS += -fomit-frame-pointer
x86_debug: debug
makefile gnu-make
add a comment |
I am writing a makefile that has multiple targets for architectures and build types. When I call make I would like to be able to pass it two targets such as:
make x86 debug
Where the architecture target sets variables to be used by the build target. An architecture target might look like this:
.PHONY: x86
x86: NAME_PREFIX = x86_64_Linux
x86: TARGET_TRIPLE = x86_64-pc-linux-elf
x86: CC = gcc
x86: CFLAGS += -fomit-frame-pointer
First issue: If I run the two targets, make skips the first target since it does nothing but set variables.
$ make x86 debug
make: Nothing to be done for `x86'.
Second issue: Even if it would run the first target, my understanding is that the values won't be retained when make starts running the second target anyway.
For now, I'm using targets that set variables and add the build type target as a prerequisite at the end of the target body, but I would end up needing a target for every combination of architecture and build type.
.PHONY: x86_debug
x86_debug: NAME_PREFIX = x86_64_Linux
x86_debug: TARGET_TRIPLE = x86_64-pc-linux-elf
x86_debug: CC = gcc
x86_debug: CFLAGS += -fomit-frame-pointer
x86_debug: debug
makefile gnu-make
add a comment |
I am writing a makefile that has multiple targets for architectures and build types. When I call make I would like to be able to pass it two targets such as:
make x86 debug
Where the architecture target sets variables to be used by the build target. An architecture target might look like this:
.PHONY: x86
x86: NAME_PREFIX = x86_64_Linux
x86: TARGET_TRIPLE = x86_64-pc-linux-elf
x86: CC = gcc
x86: CFLAGS += -fomit-frame-pointer
First issue: If I run the two targets, make skips the first target since it does nothing but set variables.
$ make x86 debug
make: Nothing to be done for `x86'.
Second issue: Even if it would run the first target, my understanding is that the values won't be retained when make starts running the second target anyway.
For now, I'm using targets that set variables and add the build type target as a prerequisite at the end of the target body, but I would end up needing a target for every combination of architecture and build type.
.PHONY: x86_debug
x86_debug: NAME_PREFIX = x86_64_Linux
x86_debug: TARGET_TRIPLE = x86_64-pc-linux-elf
x86_debug: CC = gcc
x86_debug: CFLAGS += -fomit-frame-pointer
x86_debug: debug
makefile gnu-make
I am writing a makefile that has multiple targets for architectures and build types. When I call make I would like to be able to pass it two targets such as:
make x86 debug
Where the architecture target sets variables to be used by the build target. An architecture target might look like this:
.PHONY: x86
x86: NAME_PREFIX = x86_64_Linux
x86: TARGET_TRIPLE = x86_64-pc-linux-elf
x86: CC = gcc
x86: CFLAGS += -fomit-frame-pointer
First issue: If I run the two targets, make skips the first target since it does nothing but set variables.
$ make x86 debug
make: Nothing to be done for `x86'.
Second issue: Even if it would run the first target, my understanding is that the values won't be retained when make starts running the second target anyway.
For now, I'm using targets that set variables and add the build type target as a prerequisite at the end of the target body, but I would end up needing a target for every combination of architecture and build type.
.PHONY: x86_debug
x86_debug: NAME_PREFIX = x86_64_Linux
x86_debug: TARGET_TRIPLE = x86_64-pc-linux-elf
x86_debug: CC = gcc
x86_debug: CFLAGS += -fomit-frame-pointer
x86_debug: debug
makefile gnu-make
makefile gnu-make
edited Nov 15 '18 at 18:03
superb owl
asked Nov 15 '18 at 17:24
superb owlsuperb owl
153
153
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Your second approach is better. But you don't need a target for every combination, if you use a consistent naming scheme, and use wildcard targets:
.PHONY: x86_% arm_% # etc
.PHONY: %_debug %_optimised # etc
x86_%: NAME_PREFIX = x86_64_Linux
x86_%: TARGET_TRIPLE = x86_64-pc-linux-elf
x86_%: CC = gcc
x86_%: CFLAGS += -fomit-frame-pointer
arm_%: NAME_PREFIX = ARM_LINUX
arm_%: TARGET_TRIPLE = arm-linux-eabi
arm_%: CC = arm-linux-gcc
%_debug: CFLAGS += -g
%_optimised: CFLAGS += -O3
I modified my makefile accordingly but make doesn't seem to recognize % as a wildcard character. I'm using GNU Make 3.82.
– superb owl
Nov 15 '18 at 18:12
I'm sure I've done something like that before. You did include a target with rules, right?
– Toby Speight
Nov 15 '18 at 18:34
Yes, the build targets have rules. I'm simply gettingmake: *** No rule to make target 'x86_debug'. Stop.
despite having the targetx86_%
as in the example and a%_debug
target that calls the compiler.
– superb owl
Nov 15 '18 at 19:08
1
I figured out the problem. I had an additional targetclean
which I changed to%_clean
so I could use it in place of my build type target. My%_debug
target runsclean
before compiling, so I changed the prerequisite to_clean
. The problem is that the stem can't be a zero-length string, so_clean
doesn't match%_clean
. I would have thought make would give me a more helpful error like "No rule to make target _clean required by %_debug" or something like that.
– superb owl
Nov 15 '18 at 19:43
An error like that doesn't quite make sense. A pattern is a hint about one possible way to build a target. You can have lots of patterns that could be used to build the same target (consider%.o
as a pattern target...!) so if a given pattern doesn't match, make can't say that it's an error. It goes to the next one. Only after no patterns match can make conclude that no rule exists to build a given target. It can't spit out errors for each prerequisite that couldn't be built for each pattern rule that matches a target.
– MadScientist
Nov 15 '18 at 22:42
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324864%2fhow-can-i-run-two-makefile-targets-such-that-the-second-uses-the-firsts-variabl%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your second approach is better. But you don't need a target for every combination, if you use a consistent naming scheme, and use wildcard targets:
.PHONY: x86_% arm_% # etc
.PHONY: %_debug %_optimised # etc
x86_%: NAME_PREFIX = x86_64_Linux
x86_%: TARGET_TRIPLE = x86_64-pc-linux-elf
x86_%: CC = gcc
x86_%: CFLAGS += -fomit-frame-pointer
arm_%: NAME_PREFIX = ARM_LINUX
arm_%: TARGET_TRIPLE = arm-linux-eabi
arm_%: CC = arm-linux-gcc
%_debug: CFLAGS += -g
%_optimised: CFLAGS += -O3
I modified my makefile accordingly but make doesn't seem to recognize % as a wildcard character. I'm using GNU Make 3.82.
– superb owl
Nov 15 '18 at 18:12
I'm sure I've done something like that before. You did include a target with rules, right?
– Toby Speight
Nov 15 '18 at 18:34
Yes, the build targets have rules. I'm simply gettingmake: *** No rule to make target 'x86_debug'. Stop.
despite having the targetx86_%
as in the example and a%_debug
target that calls the compiler.
– superb owl
Nov 15 '18 at 19:08
1
I figured out the problem. I had an additional targetclean
which I changed to%_clean
so I could use it in place of my build type target. My%_debug
target runsclean
before compiling, so I changed the prerequisite to_clean
. The problem is that the stem can't be a zero-length string, so_clean
doesn't match%_clean
. I would have thought make would give me a more helpful error like "No rule to make target _clean required by %_debug" or something like that.
– superb owl
Nov 15 '18 at 19:43
An error like that doesn't quite make sense. A pattern is a hint about one possible way to build a target. You can have lots of patterns that could be used to build the same target (consider%.o
as a pattern target...!) so if a given pattern doesn't match, make can't say that it's an error. It goes to the next one. Only after no patterns match can make conclude that no rule exists to build a given target. It can't spit out errors for each prerequisite that couldn't be built for each pattern rule that matches a target.
– MadScientist
Nov 15 '18 at 22:42
add a comment |
Your second approach is better. But you don't need a target for every combination, if you use a consistent naming scheme, and use wildcard targets:
.PHONY: x86_% arm_% # etc
.PHONY: %_debug %_optimised # etc
x86_%: NAME_PREFIX = x86_64_Linux
x86_%: TARGET_TRIPLE = x86_64-pc-linux-elf
x86_%: CC = gcc
x86_%: CFLAGS += -fomit-frame-pointer
arm_%: NAME_PREFIX = ARM_LINUX
arm_%: TARGET_TRIPLE = arm-linux-eabi
arm_%: CC = arm-linux-gcc
%_debug: CFLAGS += -g
%_optimised: CFLAGS += -O3
I modified my makefile accordingly but make doesn't seem to recognize % as a wildcard character. I'm using GNU Make 3.82.
– superb owl
Nov 15 '18 at 18:12
I'm sure I've done something like that before. You did include a target with rules, right?
– Toby Speight
Nov 15 '18 at 18:34
Yes, the build targets have rules. I'm simply gettingmake: *** No rule to make target 'x86_debug'. Stop.
despite having the targetx86_%
as in the example and a%_debug
target that calls the compiler.
– superb owl
Nov 15 '18 at 19:08
1
I figured out the problem. I had an additional targetclean
which I changed to%_clean
so I could use it in place of my build type target. My%_debug
target runsclean
before compiling, so I changed the prerequisite to_clean
. The problem is that the stem can't be a zero-length string, so_clean
doesn't match%_clean
. I would have thought make would give me a more helpful error like "No rule to make target _clean required by %_debug" or something like that.
– superb owl
Nov 15 '18 at 19:43
An error like that doesn't quite make sense. A pattern is a hint about one possible way to build a target. You can have lots of patterns that could be used to build the same target (consider%.o
as a pattern target...!) so if a given pattern doesn't match, make can't say that it's an error. It goes to the next one. Only after no patterns match can make conclude that no rule exists to build a given target. It can't spit out errors for each prerequisite that couldn't be built for each pattern rule that matches a target.
– MadScientist
Nov 15 '18 at 22:42
add a comment |
Your second approach is better. But you don't need a target for every combination, if you use a consistent naming scheme, and use wildcard targets:
.PHONY: x86_% arm_% # etc
.PHONY: %_debug %_optimised # etc
x86_%: NAME_PREFIX = x86_64_Linux
x86_%: TARGET_TRIPLE = x86_64-pc-linux-elf
x86_%: CC = gcc
x86_%: CFLAGS += -fomit-frame-pointer
arm_%: NAME_PREFIX = ARM_LINUX
arm_%: TARGET_TRIPLE = arm-linux-eabi
arm_%: CC = arm-linux-gcc
%_debug: CFLAGS += -g
%_optimised: CFLAGS += -O3
Your second approach is better. But you don't need a target for every combination, if you use a consistent naming scheme, and use wildcard targets:
.PHONY: x86_% arm_% # etc
.PHONY: %_debug %_optimised # etc
x86_%: NAME_PREFIX = x86_64_Linux
x86_%: TARGET_TRIPLE = x86_64-pc-linux-elf
x86_%: CC = gcc
x86_%: CFLAGS += -fomit-frame-pointer
arm_%: NAME_PREFIX = ARM_LINUX
arm_%: TARGET_TRIPLE = arm-linux-eabi
arm_%: CC = arm-linux-gcc
%_debug: CFLAGS += -g
%_optimised: CFLAGS += -O3
answered Nov 15 '18 at 17:28
Toby SpeightToby Speight
17.1k134367
17.1k134367
I modified my makefile accordingly but make doesn't seem to recognize % as a wildcard character. I'm using GNU Make 3.82.
– superb owl
Nov 15 '18 at 18:12
I'm sure I've done something like that before. You did include a target with rules, right?
– Toby Speight
Nov 15 '18 at 18:34
Yes, the build targets have rules. I'm simply gettingmake: *** No rule to make target 'x86_debug'. Stop.
despite having the targetx86_%
as in the example and a%_debug
target that calls the compiler.
– superb owl
Nov 15 '18 at 19:08
1
I figured out the problem. I had an additional targetclean
which I changed to%_clean
so I could use it in place of my build type target. My%_debug
target runsclean
before compiling, so I changed the prerequisite to_clean
. The problem is that the stem can't be a zero-length string, so_clean
doesn't match%_clean
. I would have thought make would give me a more helpful error like "No rule to make target _clean required by %_debug" or something like that.
– superb owl
Nov 15 '18 at 19:43
An error like that doesn't quite make sense. A pattern is a hint about one possible way to build a target. You can have lots of patterns that could be used to build the same target (consider%.o
as a pattern target...!) so if a given pattern doesn't match, make can't say that it's an error. It goes to the next one. Only after no patterns match can make conclude that no rule exists to build a given target. It can't spit out errors for each prerequisite that couldn't be built for each pattern rule that matches a target.
– MadScientist
Nov 15 '18 at 22:42
add a comment |
I modified my makefile accordingly but make doesn't seem to recognize % as a wildcard character. I'm using GNU Make 3.82.
– superb owl
Nov 15 '18 at 18:12
I'm sure I've done something like that before. You did include a target with rules, right?
– Toby Speight
Nov 15 '18 at 18:34
Yes, the build targets have rules. I'm simply gettingmake: *** No rule to make target 'x86_debug'. Stop.
despite having the targetx86_%
as in the example and a%_debug
target that calls the compiler.
– superb owl
Nov 15 '18 at 19:08
1
I figured out the problem. I had an additional targetclean
which I changed to%_clean
so I could use it in place of my build type target. My%_debug
target runsclean
before compiling, so I changed the prerequisite to_clean
. The problem is that the stem can't be a zero-length string, so_clean
doesn't match%_clean
. I would have thought make would give me a more helpful error like "No rule to make target _clean required by %_debug" or something like that.
– superb owl
Nov 15 '18 at 19:43
An error like that doesn't quite make sense. A pattern is a hint about one possible way to build a target. You can have lots of patterns that could be used to build the same target (consider%.o
as a pattern target...!) so if a given pattern doesn't match, make can't say that it's an error. It goes to the next one. Only after no patterns match can make conclude that no rule exists to build a given target. It can't spit out errors for each prerequisite that couldn't be built for each pattern rule that matches a target.
– MadScientist
Nov 15 '18 at 22:42
I modified my makefile accordingly but make doesn't seem to recognize % as a wildcard character. I'm using GNU Make 3.82.
– superb owl
Nov 15 '18 at 18:12
I modified my makefile accordingly but make doesn't seem to recognize % as a wildcard character. I'm using GNU Make 3.82.
– superb owl
Nov 15 '18 at 18:12
I'm sure I've done something like that before. You did include a target with rules, right?
– Toby Speight
Nov 15 '18 at 18:34
I'm sure I've done something like that before. You did include a target with rules, right?
– Toby Speight
Nov 15 '18 at 18:34
Yes, the build targets have rules. I'm simply getting
make: *** No rule to make target 'x86_debug'. Stop.
despite having the target x86_%
as in the example and a %_debug
target that calls the compiler.– superb owl
Nov 15 '18 at 19:08
Yes, the build targets have rules. I'm simply getting
make: *** No rule to make target 'x86_debug'. Stop.
despite having the target x86_%
as in the example and a %_debug
target that calls the compiler.– superb owl
Nov 15 '18 at 19:08
1
1
I figured out the problem. I had an additional target
clean
which I changed to %_clean
so I could use it in place of my build type target. My %_debug
target runs clean
before compiling, so I changed the prerequisite to _clean
. The problem is that the stem can't be a zero-length string, so _clean
doesn't match %_clean
. I would have thought make would give me a more helpful error like "No rule to make target _clean required by %_debug" or something like that.– superb owl
Nov 15 '18 at 19:43
I figured out the problem. I had an additional target
clean
which I changed to %_clean
so I could use it in place of my build type target. My %_debug
target runs clean
before compiling, so I changed the prerequisite to _clean
. The problem is that the stem can't be a zero-length string, so _clean
doesn't match %_clean
. I would have thought make would give me a more helpful error like "No rule to make target _clean required by %_debug" or something like that.– superb owl
Nov 15 '18 at 19:43
An error like that doesn't quite make sense. A pattern is a hint about one possible way to build a target. You can have lots of patterns that could be used to build the same target (consider
%.o
as a pattern target...!) so if a given pattern doesn't match, make can't say that it's an error. It goes to the next one. Only after no patterns match can make conclude that no rule exists to build a given target. It can't spit out errors for each prerequisite that couldn't be built for each pattern rule that matches a target.– MadScientist
Nov 15 '18 at 22:42
An error like that doesn't quite make sense. A pattern is a hint about one possible way to build a target. You can have lots of patterns that could be used to build the same target (consider
%.o
as a pattern target...!) so if a given pattern doesn't match, make can't say that it's an error. It goes to the next one. Only after no patterns match can make conclude that no rule exists to build a given target. It can't spit out errors for each prerequisite that couldn't be built for each pattern rule that matches a target.– MadScientist
Nov 15 '18 at 22:42
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324864%2fhow-can-i-run-two-makefile-targets-such-that-the-second-uses-the-firsts-variabl%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown