aostruszka / nonrec-make

Non-recursive make template for GNU make

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SUBDIRS question

xulfir opened this issue · comments

First off, thanks for sharing this. It's so easy to use.

I've noticed one bit of odd behavior though. I tried to create a unit test target in a subdir of a directory with a static library target, but the unit test would never get built. Stranger still, the unit test's obj directory is visible in the output when I run make clean_tree.

I created a simpler test that will show what I am talking about. Using the current master (e486147), I modified Rules.top to point to a new ex3 directory with these files:

ex3/hello.cpp
ex3/Rules.mk
ex3/goodbye/goodbye.cpp
ex3/goodbye/Rules.mk

hello.cpp and goodbye.cpp are just simple "Hello world" files. The contents of the two Rules.mk files are:

$ cat ex3/Rules.mk
TARGETS := hello
SUBDIRS := goodbye

hello_DEPS = hello.o
$ cat ex3/goodbye/Rules.mk
TARGETS := goodbye

goodbye_DEPS = goodbye.o

When I run make, I only get this:

$ make
Rules generated...
CXX /home/xulfir/nonrec-make/ex3/hello.cpp
LINK /home/xulfir/nonrec-make/ex3/obj/hello

When I run make clean_tree I see this though.

$ make clean_tree
Rules generated...
rm -f /home/xulfir/nonrec-make/obj/*
rm -f /home/xulfir/nonrec-make/ex3/obj/*
rm -f /home/xulfir/nonrec-make/ex3/goodbye/obj/*

Is there something I'm doing wrong?
Thanks again!

Looks like the problem was that SUBDIRS_TGTS would only be included if there wasn't a TARGETS specified for the current directory. The patch below solved the problem for me. I'm not sure if it will break anything else, though.

diff --git a/mk/footer.mk b/mk/footer.mk
index ad6cdae..1aec6c7 100644
--- a/mk/footer.mk
+++ b/mk/footer.mk
@@ -73,11 +73,8 @@ $(foreach tgt,$(filter-out $(AUTO_TGTS),$(TARGETS_$(d))),$(eval $(call tgt_rule,
 # dir_$(d) - see below)
 tree_$(d) : $(call subtree_tgts,$(d))

-# If the directory is just for grouping its targets will be targets from
-# all subdirectories
-ifeq ($(strip $(TARGETS_$(d))),)
-TARGETS_$(d) := $(SUBDIRS_TGTS)
-endif
+# Include targets from all subdirectories
+TARGETS_$(d) := $(SUBDIRS_TGTS) $(TARGETS_$(d))

 # This is a default rule - see Makefile
 dir_$(d) : $(TARGETS_$(d))

This is by "design".
By default simple make should build only targets from directory where it was invoked - any targets from subdirectories should be built only if they are dependencies of targets from this directory.
To build all subtree targets as of now you can run in shell: "make tree_$(pwd)"
This should build all targets from given subtree even if they are not dependencies. However your comment caused me to check this and I will change current implementation since it can be done better as the clean_tree is. Moreover I will add a shortcut in Makefile so that simple "make tree" will be enough for what you need.

Best regards
Andrzej

PS. The part that you have changed is for the case where you split code over couple subdirectories. Then in the parent you can have Rules.mk with only "SUBDIRS ..." line and targets of this parent will be all targets from its subdirs.

Thanks for explaining, Andrzej. I was assuming the SUBDIRS were being used as a means to collect all of the Rules.mk and targets to be built.

This brings up another question: If I wanted to explicitly call out goodbye as a build target (but keep the same directory layout), what would you recommend? This seems a bit tricky since TARGETS need to be relative to the OBJDIR, right? I tried adding ../goodbye/$(OBJDIR)/goodbye to ex3\Rules.mk's TARGETS, but I get an error because ex3/obj/../goodbye/obj/goodbye and ex3/goodbye/obj/goodbye aren't the same to make. The cleanest way I can think of is to put ex3/goodbye in the SUBDIRS of the parent directory's Rules (Rules.top in this case).

If you want to call out compilation of only 'goodbye' then there are basically two choices. BTW I intended to point you to wiki pages on that, but ... it turns out that some part of [[Basic Concepts]] got lost when I moved from google to github. It is fixed now (points 5&6) and you might want to read it again.

  1. "make /full/path/to/goodbye/including/objdirs" - not very convenient unless you define in shell some variable that will hold the full path. However this will not survive beyond your current shell instance so my preference would be to ...
  2. Add an alias in Rules.mk in ex3 directory
    • e.g. "tests : dir_$(d)"
    • or "goodbye : $(OBJPATH)/goodbye"

Since you've indicated that this was meant to be some unit test the first case would cause "make tests" to build all targets from ex3 directory. Second case is just an alias for one particular test and with it you can just issue "make goodbye".

HTH
Andrzej

PS. I've uploaded new commit (50b724b) with this "tree" target, so I'm closing this "issue" :)

Xulfir
I just thought that my answer might not be what you were looking for.
It depends on what you've meant by "I wanted to explicitly call out goodbye as a build target".
If by "call out" you've meant having ability to specify goodbye as a target during make invocation then the above answer should help. However if by "call out" you've meant using goodbye as e.g. some dependency inside Rules.mk then if this is in parents directory Rules.mk simple SUBDIRS_TGTS or TARGETS_$(subdir) should work. However if you want to refer to just one of the targets or using full path is inconvenient then using alias should be sufficient e.g. in ex3/Rules.mk just have
GB := $(OBJPATH)/goodbye
and you can use this variable.
This has some gotchas though so let me know if this answers your needs.
Best regards
Andrzej

Thanks again, Andrzej. This has been very helpful. It is clear you've put a lot of thought into this project. I'm very glad you have shared it.
Cheers,
Xulfir