On Tue, 7 Oct 2025 at 02:17, Michael Gasanenko <[email protected]> wrote:
> >If you don't know where your files are, then what are you doing, really?
>
> Really: I was testing for compatibility with multiple build systems.
>
> The problem is not that I don't know where some file is, the problem is
> that my script runs at different depths.
This seems like an X-Y problem.
- “I need to find foo/bar/qux in the current hierarchy”
- “I know, I'll just look at ., .., ../.., etc until I find it”
- “Umm; now how do I do *that*?”
As others have noted, it's likely that the important point is to exactly
locate the top level directory.
However I agree, using absolute paths is not always convenient, especially
when you want to pre-arrange a tree that others can unpack..
One way is to install a symbolic link in each directory and then just use
that:
ln -s . .top
find */ -type d -exec ln -s ../.top {} \;
Obviously this doesn't work on old systems that don't support symbolic
links; just avoid fatfs.
But on any reasonable system, this is by far the simplest and most reliable
approach.
However if it's more complex, you can have separate symlinks for different
parts, such as "this source", "top source", and "top build"; that's
probably appropriate in your case since you're comparing different
approaches.
Another way is to pass these paths between processes using an option or
environment variable.
In that case you simply need to preface the “current” top indicator with
“../” when you run something in an immediate subdirectory.
In a buildscript you might write something like this:
top= up=../
export up
for part in */buildscript
do
(
dir=${part%/*}
up2=${dir//+([!/])\//$up} # if iterating over buildscripts at different
depths
cd "$dir" &&
exec top=$up2$top ./buildscript "$@"
)
done
Or in a Makefile you might write something like this:
top =
up = ../
PARTS1 = dir1/foo dir2/bar dir3/zot
PARTS2 = dir1/a/qux dir1/b/qux
parts:: $(PARTS1) $(PARTS2)
$(PARTS1):
$(MAKE) -C $(@D) top=$(up)$(top) up=$(up) $(@F)
$(PARTS2):
$(MAKE) -C $(@D) top=$(up)$(up)$(top) up=$(up) $(@F)
Autoconf needs some arm-twisting to be convinced to use relative rather
than absolute paths; it helps to start with:
../src/configure
since it will use its own invocation path to get the prefix path to the
source directory.
However you'd also need to have edited Makefile.in to ensure it uses “../”
to refer to the parent build dir, and src=../$(parent_src)thissubdir/ to
refer to its corresponding source directory.
-Martin