0

I've projects which include makefile under the root project

like

project
 -Folder1
 -Folder2
 -app1
 -service1
 -Makefile

Now I want inside the makefile loop on the folders (under the root. i.e. under project) and print the folder name, the tricky part here that the folders can be changed from projectX to projectN , is it possible?

07_05_GuyT
  • 2,787
  • 14
  • 43
  • 88
  • Is this what you are looking for? [Listing only directories using ls in bash](https://stackoverflow.com/questions/14352290/listing-only-directories-using-ls-in-bash-an-examination) – arjoonn Sep 12 '18 at 06:34
  • @arjoonn - can I use it for looping on directory and provide each name ?, I need the `loop` – 07_05_GuyT Sep 12 '18 at 06:37
  • sure. `ls -d */` should list the directories and you can loop over it using something like `for dir in */; do echo $dir; done` – arjoonn Sep 12 '18 at 06:40
  • 1
    @arjoonn: beware that `ls -d */` will exit with non-zero status if there are no directories to list. Maybe not what is expected in a make recipe. Moreover, looping in recipes using the shell's loop constructs is not really the _make way_. – Renaud Pacalet Sep 12 '18 at 06:45

1 Answers1

1

Something like this?

$ tree -F    
.
|-- Folder1/
|-- Folder2/
|-- Makefile
|-- app1
`-- service1

2 directories, 3 files

$ cat Makefile
DIRS     := $(notdir $(shell find . -mindepth 1 -maxdepth 1 -type d))
DIRNAMES := $(addprefix print-folder-name-,$(DIRS))

.PHONY: $(DIRNAMES) print-folder-names

print-folder-names: $(DIRNAMES)

$(DIRNAMES): print-folder-name-%:
    @printf '%s\n' '$*'

$ make print-folder-names
Folder2
Folder1

Everything is quite simple and easy to understand. The only subtlety is probably the static pattern rule $(DIRNAMES): print-folder-name-%:. It is equivalent to one single rule per folder:

print-folder-name-Folder1:
    @printf '%s\n' 'Folder1'

print-folder-name-Folder2:
    @printf '%s\n' 'Folder2'

Of course, in the same rule you can do anything you like (else than printing its name) for each folder; just adapt the recipe. The $* automatic variable expands as the stem of the pattern (the folder name in this case).

EDIT: if you also want to print something special for some folders, you can also use target-specific make variables:

$ cat Makefile
...
print-folder-name-Folder2: SOMEMORETEXT := ' foo'

$(DIRNAMES): print-folder-name-%:
    @printf '%s%s\n' '$*' $(SOMEMORETEXT)

$ make print-folder-names
Folder2 foo
Folder1
Renaud Pacalet
  • 25,260
  • 3
  • 34
  • 51
  • Thank you 1+ , can it's done by loop. the idea that I need to run on the list of folder in the make file and for each folder I need to execute some command... – 07_05_GuyT Sep 12 '18 at 06:49
  • Make loops naturally. In the example I show it loops over each folder and prints its name. The static pattern rule is the same as if you had one separate rule for each folder, with the same recipe. Look maybe at [this other answer](https://stackoverflow.com/questions/52282549/for-each-on-target-of-makefile-variable/52284299#52284299) for an explanation. – Renaud Pacalet Sep 12 '18 at 06:50
  • Not sure that I got it ...:( yours example is working great excepts that I need a loop which prints each folder name... – 07_05_GuyT Sep 12 '18 at 06:52
  • something that `@printf '%s\n' '$*'` this will be inside the loop..., is it possible? – 07_05_GuyT Sep 12 '18 at 06:53
  • `@printf '%s\n' '$*'` is already inside a loop: the loop that make will create to build all targets listed in the `$(DIRNAMES)` variable. – Renaud Pacalet Sep 12 '18 at 06:54
  • yes I understand :) , is there a way to convert it somehow to for each loop? – 07_05_GuyT Sep 12 '18 at 06:56
  • Sorry, I don't understand why you want to add another loop and what it could be used for. Is this an assignment in a course about shell programming? – Renaud Pacalet Sep 12 '18 at 06:59
  • ohh, no it isn't I just want to use `if else` inside for example if `foldername==Folder2` print `foo` else `bar` and I think in the way it's a bit more complicated – 07_05_GuyT Sep 12 '18 at 07:01
  • I really don't understand. Do you want to print the name of the folder, `foo`, `bar`, or something else? – Renaud Pacalet Sep 12 '18 at 07:05
  • Sorry, I want to print the name of the folder, but in case I've folder for example 'folder2` print the name and just for it add `foo` , I just want to understand how it can be done in a bit more complex secnarion when I need the value of the variable ... – 07_05_GuyT Sep 12 '18 at 07:07
  • Edited my answer to show you how to do this with make target-specific variables. – Renaud Pacalet Sep 12 '18 at 07:10
  • sorry I need to leave for a while, now im checking it. – 07_05_GuyT Sep 12 '18 at 08:03
  • btw, when i use the code it even prints hidden folders such as `.idea` or .`git` etc, can it be avoided simply ? – 07_05_GuyT Sep 12 '18 at 08:07
  • @shopiaT: sure, just adapt the `find` options to skip `.*` names (I am sure SO already has answers to this other question). – Renaud Pacalet Sep 12 '18 at 08:19
  • Thanks a lot I've closed the question :) , it will be great if you can elborate just on the first `-mindepth 1 -maxdepth 1` and the second `addprefix` what the purpose for them – 07_05_GuyT Sep 12 '18 at 08:22
  • I think I understand, not going deeper to other folders inside recursive – 07_05_GuyT Sep 12 '18 at 08:25