Skip to content

Instantly share code, notes, and snippets.

@dmarx
Created June 21, 2017 20:31
Show Gist options
  • Select an option

  • Save dmarx/167aa38812b82fd14fedac566a90a9fa to your computer and use it in GitHub Desktop.

Select an option

Save dmarx/167aa38812b82fd14fedac566a90a9fa to your computer and use it in GitHub Desktop.
Minimal working example for stackoverlfow question
DIRS := $(filter dir%, $(shell ls))
foo_sources := $(wildcard */source/foo.a)
foo_targets_prt := $(patsubst %.a, %.b, $(foo_sources))
foo_targets := $(subst source,target, $(foo_targets_prt))
bar_sources := $(wildcard */source/bar.a)
bar_x := $(patsubst %/bar.a, %/Y.a, $(bar_sources))
bar_y := $(patsubst %/bar.a, %/Z.a, $(bar_sources))
bar_targets := $(bar_x) $(bar_y)
all:
$(MAKE) rebuild
$(MAKE) foos
$(MAKE) bars
##################################################
#### 1. dir%/source/foo.a > dir%/target/foo.b ####
##################################################
foos: $(foo_targets)
### This is the general idea
#target/%.b: source/%.a
# operation1.sh $< $@
#$(foo_targets):$(foo_sources)
# operation1.sh $< $@
#$(DIRS)/target/%.b: $(DIRS)/source/%.a
# operation1.sh $< $@
#$(foo_targets): $$(@D)/foo.a
# operation1.sh $< $@
$(foo_targets): $(subst target,source, $(patsubst %.b, %.a, $@))
$(eval dep := $(subst target,source, $(patsubst %.b, %.a, $@)) )
echo $(dep)
./operation1.sh $(dep) $@
################################################
#### 2. dir%/source/bar.a > dir%/source/Y.a ####
#### > dir%/source/Z.a ####
################################################
bars: $(bar_targets)
### This is the general idea
#%/source/Y.a %/source/Z.a: %/source/bar.a
# operation2.sh $< $@
#$(DIRS)/source/Y.a: $(patsubst %/source/Y.a, %/source/bar.a, $@)
# operation2.sh $< $(patsubst %/source/bar.a, %, $@)
### $(dirname $@) doesn't evaluate to anything
#$(bar_targets): $(dirname $@)/source/bar.a
# ./operation2.sh $< $@
$(bar_targets): $(filter %bar.a, $(patsubst dir%/source/Y.a, dir%/source/bar.a, $@) $(patsubst dir%/source/Z.a, dir%/source/bar.a, $@))
$(eval dep := $(filter %bar.a, $(patsubst dir%/source/Y.a, dir%/source/bar.a, $@) $(patsubst dir%/source/Z.a, dir%/source/bar.a, $@)) )
./operation2.sh $(dep) $@
##################################################################
build_demo:
mkdir dir0
mkdir dir1
mkdir dir2
mkdir dir0/source
mkdir dir1/source
mkdir dir2/source
mkdir dir0/target
mkdir dir1/target
mkdir dir2/target
head -c 32 /dev/urandom > dir0/source/foo.a
head -c 32 /dev/urandom > dir1/source/foo.a
# head -c 32 /dev/urandom > dir2/source/foo.a
# head -c 32 /dev/urandom > dir0/source/bar.a
head -c 32 /dev/urandom > dir1/source/bar.a
head -c 32 /dev/urandom > dir2/source/bar.a
echo -e $$'#!/bin/sh\n\nhead $$1 -c 10 > $$2' > operation1.sh
echo -e $$'#!/bin/sh\n\nhead $$1 -c 10 > $$(dirname $$2)/Y.a\ntail $$1 -c 10 > $$(dirname $$2)/Z.a' > operation2.sh
echo -e $$'#!/bin/sh\n\necho "1:" $$1\necho "2:" $$2' > operation3.sh
chmod +x operation*.sh
delete:
find . -type d -name 'dir*' -exec rm -r {} +
find . -type f ! -name 'Makefile' -exec rm {} +
rebuild:
$(MAKE) delete
$(MAKE) build_demo
debug:
echo
echo $(foo_sources)
echo $(foo_targets_prt)
echo $(foo_targets)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment