MAKEPP_REPOSITORIES(1)

πŸ“¦ version 2.0.98.5, in debian bookworm, ubuntu jammy, ubuntu noble

πŸŽ€ MAKEPP_REPOSITORIES πŸŽ€

okay bestie, welcome to makepp_repositories(1), a real one from the Makepp manual set. sit down, grab a boba, we're doing repositories. πŸ’…


✨ NAME ✨

makepp_repositories β€” how to use repositories for variant builds, for keeping a central set of sources, and other cute little things πŸ¦‹


🌈 DESCRIPTION 🌈

so a repository is literally just a directory or a whole directory hierarchy that lives outside your default directory, and it holds files that your makefile needs in the current directory tree. makepp can automatically link files from the repository into the current directory tree whenever it needs them. repositories do the same kind of job as the VPATH variable, but β€” and this is the iconic part β€” unlike VPATH in other versions of make, you do not have to do anything special to your makefile to make them work. no cap. πŸ‘‘

you specify repositories with the -R or --repository command line option, or with the repository statement in the makefile. ⚠️ quick heads up though: if you have a habit of calling makepp in different subdirectories of your build tree, it is easy to accidentally reimport a repository somewhere else. as a safeguard against this, if you use RootMakeppfile, makepp will refuse to start if it finds one above or below where it would be imported. dramatic? yes. protective? also yes. 😀

this is somewhat comparable to operating system union filesystems (unionfs...) β€” the current directory is like the highest level writable layer, and all repositories are like lower read-only layers. it's giving layer cake but make it filesystem. 🍰

repositories are useful in several different situations:

  • when you want to place your object and executable files in a separate directory, but the makefile is written to place them in the same directory as the sources.
  • when you want to build the same program two different ways (e.g., with two different sets of compilation options, or for two different architectures).
  • when you don't have write access to all or part of the source tree.
  • when several developers are working on the same project, and there is a common source repository containing all the sources for the project. each developer can modify only the files he needs to change in his local directory without affecting the other developers, and makepp will automatically fetch the unmodified files from the source repository. 🫢

makepp's implementation of repositories does not require rewriting of the build commands at all, unlike (for example) repositories in cons. makepp puts a symbolic link into the directory where the command is expecting it. as long as the command does not refer to absolute directories, the exact same shell command will work with files from a repository. so it works not only for compilation commands, but any kind of command you can think to put in your makefile. periodt. ✨

makepp also has another kind of mechanism called a build cache, which solves some of the same sorts of problems as repositories but in a different way. depending on your problem, a build cache may be more useful than a repository. see makepp_build_cache for information about build caches and a comparison of build caches and repositories. πŸ‘€


πŸ¦‹ Examples πŸ¦‹

repositories are best explained by several examples of what you can do. let's go. πŸš€


🌸 Different compilation options 🌸

suppose you have a simple program with a makefile that looks something like this:

CFLAGS      = -O2
OBJECTS = a.o b.o c.o
my_program: $(OBJECTS)
    cc $(inputs) -o $(output)

%.o: %.c
    cc $(CFLAGS) -c $(input) -o $(output)

this makefile places the files a.o, b.o, c.o, and my_program in the same directory as the source files. 🫠

sometimes you want to place the binary files into a separate directory. for example, you might build your program on several different architectures, and you don't want the binary files on one architecture to be replaced with the binary files on the other. or you might want to make a temporary change and recompile without wiping out the previous compilation results. without repositories, you would have to modify your makefile to place the objects elsewhere. exhausting. 😭

with a repository, however, you don't have to touch your makefile at all. consider the following sequence of commands:

% cd my_program_source
% makepp                    # Builds using the above makefile, and
                            # object files go into the directory
                            # my_program_source.
% cd ..
% mkdir binary-debug        # Make a clean directory for building the
% cd binary-debug           # same program with different options.
% makepp -R ../my_program_source CFLAGS=-g
                            # Now objects go into binary-debug.

the first makepp command compiles the source files with optimization and puts the objects into the directory my_program_source, because that's what the makefile is supposed to do. now we want to rebuild the program, but we want to change the value of CFLAGS to compile for debug. we specify the new value of CFLAGS on the command line, and we also tell makepp that the my_program_source directory is a repository using the -R option. πŸ’–

every time makepp realizes that it needs a file that it doesn't already have in the current directory, it looks in the repository. in this case, it first looks for the makefile, which doesn't exist in the binary-debug subdirectory. so it creates a symbolic link to it from the makefile in my_program_source, and then reads in the makefile. then it notices that it needs the file a.c in order to build a.o, and so it links in a.c from the repository. if a.c includes any files contained in my_program_source, then these will be automatically linked in as well.

πŸ“Œ note: those links are useful for things like debugging, but if you don't like them, makeppclean -R can remove them.

running the build command in binary-debug won't touch any of the files in my_program_source. thus from the same set of source files, you now have two different copies of the program, one compiled with optimization and one compiled for debug. and this happened without touching the makefile at all. iconic behavior. πŸ‘‘

the advantage of using repositories instead of simply recompiling and overwriting the original binaries is that now if we fix our bugs and want to go back to the optimized version, we don't have to recompile everything. since the original object files are still around, and most of them are still valid, we can save a lot of time on recompilation. this does not make a big difference when only three source files are involved, but for a larger build that takes minutes or hours to complete, the savings in programmer time and frustration can be significant. πŸ”₯


πŸ’œ Rebuilding one file with a minor modification to the compilation commands πŸ’œ

makepp doesn't fetch only source files from the repository. if the object files in the repository don't need rebuilding, it will use them. for example, consider a slight modification to the above makefile:

CFLAGS   := -O2
A_CFLAGS := -O6 -funroll-loops

OBJECTS := a.o b.o c.o

my_program: $(OBJECTS)
    cc $(inputs) -o $(output)

%.o: %.c
    cc $(CFLAGS) -c $(input) -o $(output)

a.o: a.c
    cc $(A_CFLAGS) -c $(input) -o $(output)

the idea is that a.o contains the time-critical code, so it is compiled with higher optimization than the rest of the objects. now suppose we want to test just how different the timing is with different compile options. a repository can help with this, too:

% cd my_program_source
% makepp                    # Builds using the above makefile, and
                            # object files go into the directory
                            # my_program_source.
% cd ..
% mkdir no-unrolling        # Make a clean directory for building the
% cd no-unrolling           # same program with different options.
% makepp -R ../my_program_source A_CFLAGS=-O2
% cd ..
% time no-unrolling/my_program # Benchmark the two versions of the program.
% time my_program_source/my_program

makepp proceeds as before, linking in a copy of the makefile and then examining the object files. now only the a.o module needs recompiling, since the options for b.o and c.o haven't changed. makepp notices that it can use b.o and c.o from the repository, so it just links those in. however, it will recompile a.o in the no-unrolling directory. once the compilation is finished, the two different versions of the program can be benchmarked. benchmark era. πŸ“Šβœ¨


πŸ¦„ Rebuilding with a minor modification to the source πŸ¦„

now suppose we want to make a change to a.c and benchmark the program before and after the change. repositories can help again. consider this sequence of commands:

% mkdir modified-a
% cp my_program_source/a.c modified-a
% cd modified-a
% emacs a.c                 # Make some modifications just to this module.
% makepp -R ../my_program_source

here we have created a new directory that just contains the single source file we want to modify. makepp now takes a.c from the modified-a subdirectory, but uses the copies of b and c from the my_program_source directory. without changing any of the binary files in my_program_source, we have created a separate copy of the program that incorporates our changes to a.c. if there are other developers using the sources in my_program_source, they will be unaffected by our changes. considerate queen behavior. 🫢

repositories can thus be used as a quick way to build variants of a program, without adding complicated conditions to the makefile. none of the files in the original directory are modified; they are used as needed. πŸ’…


🌈 Using a directory hierarchy 🌈

a repository is actually not just a single directory, it's a whole directory hierarchy. suppose you use /our/library as a repository. now /our/library may well contain many subdirectories, e.g., /our/library/gui and /our/library/network. consider this command:

% makepp -R /our/library

any commands in the makefile that refer to files in the directory ./network will actually get files from /our/library/network, and similarly for ./gui. makepp automatically creates any directories that exist in the repository but not in the current directory. nesting but make it slay. πŸͺ†


⭐ Linking to any place in the file system ⭐

all of the above examples show files from a repository being linked into the current directory or its subdirectories, but you can actually have makepp link them into any place in the file system that you have write access to. this is done by specifying -R new-location=old-location.

for example, sometimes it's a little tedious to type the following:

mkdir alternate-build
cd alternate-build
makepp -R ..

you can do it all with one command, like this:

makepp -R alternate-build=. -F alternate-build

and yeah, there's also -F (or its long-lost twin -makeppfile, same energy πŸ’…) β€” it changes to that directory before loading the makefile. But bestie, listen: you must specify -R before -F. Order matters. The command line is a queue, not a vibe ✨.

πŸ’€ Heads up though β€” this example puts the new build tree inside the repository. That will not work if you use a RootMakeppfile, because makepp safeguards against nested trees. It's also not a good idea if you use **, because if you ever build in the repository it will also find edited and generated files in this subtree. Messy. Avoid πŸ™ˆ.

okay so assigning a different location in the file system? Also lowkey useful for more complicated builds, like when you've got several library subdirectories. For example, here's a command I have used to build variants of one of my programs:

% makepp -R test-build/seescape=/src/seescape \
     -R test-build/HLib=/src/HLib \
     -R test-build/H5pp=/src/H5pp \
     -R qwt=/src/external_libraries/qwt \
     -F test-build/seescape

This command loads in files from four different repositories, and then cds to the ./test-build/seescape directory and executes the makefile there. Files contained in the directory tree beginning with /src/seescape are linked into ./test-build/seescape. In other words, makepp will temporarily link the file /src/seescape/gui/image_canvas.cxx to ./test-build/seescape/gui/image_canvas.cxx when it is needed. Iconic, honestly πŸ‘‘. And this command will work even if the test-build directory doesn't exist yet β€” makepp will create it for you. (But you must specify the -R options before the -F option on the command line. Yes, again. I'm not gonna stop saying it 🫢)

Multiple equivalent repositories 🫢

Say your project is maintained by several fairly autonomous groups. You could have one complete repository with all the sources as they are in production or at least successfully tested. Every group can have a mostly empty repository with (part of) the same structure, containing the files group members have finished developing.

Developers' current directories will have the files they are still working on. The group repository will be the first one given and the production repository the last one, so that it furnishes the files not found in the group repository:

$ makepp -R/path/to/group/repository -R/path/to/production/repository

Since this is probably fairly static for that directory, you may want to put a file .makepprc at its root with the following content:

-R/path/to/group/repository -R/path/to/production/repository

Or, presuming that it has a fixed path, you could write into your makefile:

repository /path/to/production/repository

and, because options are seen before makefiles are read, you can then call just

$ makepp -R/path/to/group/repository

Repositories as fixed part of your build system πŸ“Œ

If you know you always use some repository, you can use the repository or vpath statements in your makefile. Set it and forget it, periodt πŸ’….

Caveats with repositories ⚠️

For finding your way around your file hierarchy and for allowing the debugger to find the sources it is useful to have the links used while building. But when you want to edit a file or resync it with your version control, the links can get in the way. That is because the system traverses the link and writes to the file in the repository. Unless it's your personal repository used just for keeping things apart, that may not be what you want. 😭

As a safeguard against inadvertent overwriting of public files it is suggested to make the sources in the repository unwritable. It might even not be enough to remove the write bit, because a version control system which insists on your locking the files for editing might also do that, but temporarily make the file writable while resyncing it. If that is the case for you, the repository should actually belong to a different user. (Yeah. Whole other person. Boundaries πŸ’œ)

There are a few tactics to surmount this:

  • Keep the sources you edit in a repository, separate from your build tree. Whenever you put a file, which was previously fetched from another repository, into this editing repository, makepp will notice and fetch it from there, provided it is the first repository you specify.
  • Remember to delete any file, before you create a copy for writing. If you follow the safeguard suggestion above, forgetting to do this will give an error message when writing. To help you, the following function delink will replace one link by a copy of the linked file. The first variant is for all kinds of Bournish Shells, the second one for csh (or at least tcsh):
$ delink() { { rm $1 && cat >$1; } <$1; }
% alias delink '( rm \!:1 && cat >\!:1; ) <\!:1'
  • If you feel you don't need them, you can delete them all, whenever you want, e.g. after every makepp run, possibly backgrounded (either short or long form):
makeppclean --recurse --only-repository-links
mppc -rR

Don't build in a repository during use 🚫

A repository is meant to be read-only while it is being used as a repository. Makepp will not work properly if you change files in your repository during the course of a build. Nightly builds may be ok for you, if no one else uses the repository at that time. Before it starts the build, makepp gets a list of all the files that exist in the repository, and never updates its list, except for files it expects to appear.

If you need a repository that's changing as you build, you might want to consider makepp's build cache mechanism (see makepp_build_cache). Alternatively, you can use a "poor man's repository": you can put explicit rules into your makefile to create the soft links, like this:

%.c : $(directory_I_wish_was_a_repository)/%.c
    &ln -fs $(input) $(output)

This works only for source files; you can't easily use this to link a file if it is already built in the repository, but build it here if it's not already built, since there is only allowed to be one way to build a file. One way. That's the rule 🧚

Use only relative filenames πŸ›€οΈ

Repositories work completely transparently if the makefiles use only relative filenames. In the above example, it's ok if the makefile in /src/seescape refers to ../HLib, but the above command will not work as expected if it refers to /src/HLib. If you need to use absolute file names, you can put them into make variables and then override them on the command line, like this:

% makepp -R test-build/seescape=/src/seescape SEESCAPE=/home/holt/test-build/seescape \
     -R test-build/HLib=/src/HLib HLIB=/home/holt/test-build/HLib \
     -R test-build/H5pp=/src/H5pp H5pp=/home/holt/test-build/H5pp \
     -R qwt=/src/external_libraries/qwt QWT=/home/holt/test-build/qwt \
     -F test-build/seescape

The above will work as long as the HLib directory is referred to as $(HLIB) in all the makefiles. Note that you have to specify absolute paths for the directories, because makepp cd's to test-build/seescape before reading the makefile. This leads to long and complicated make commands; use relative paths when possible. (Your future self will thank you πŸ’–)

Makepp must know about all dependencies πŸ•΅οΈ

Repositories will not work if there are hidden dependencies that makepp doesn't know about. (In fact, doing a build using repositories is one way of checking for forgotten dependencies. But, just for this check, don't combine it with a build cache, since fetching something there, instead of building it, might hide a forgotten dependency.) Sometimes these dependencies can be fairly subtle. For example, the libtool command will not only create .lo and .la files as listed on the command line, but it also may create a subdirectory called .libs which contains the actual object files. To prevent build mistakes, makepp refuses to link in a .la file from a repository. Hopefully in the future libtool will be better supported. πŸ™

Many hidden dependencies related to compilation are caught by the command line scanner. If your compiler uses the common Unix compilation flags (e.g., -I, -D, etc.), then makepp will usually figure out where all your include files are. You may have to be careful if you have any homegrown scripts that create files that makepp doesn't know about. For correct builds, it is vitally important to list all targets and dependencies (or determine them automatically by scanning).

Putting absolute filenames into programs πŸ—‚οΈ

Repositories will also not work if any of the files built contain absolute file names in them (e.g., if any of your build commands write out an absolute filename). For example, it turns out that the .la files produced by libtool have this property. (If you look at the contents of the .la file you'll see that the dependency list contains absolute filenames.) In order to solve this particular problem, makepp will not link .la files from a repository; it will insist on rebuilding them. Non-negotiable πŸ’…

Avoid linking in unnecessary directories 🧹

Repositories can be slow on startup and use a lot of memory if there are a lot of unnecessary files in the repository. For example, if you use an automatic HTML documentation generator which makes thousands of .html files from your source code, you may not want to put them in a subdirectory of a directory that's used as a repository. It's better to put them in a different directory tree entirely, so the repository mechanism won't load in their names. Keep it lean, bestie ✨

Too Many Files 😡

The disadvantage of repositories is that symbolic links, which the repository mechanism uses, are individual files (though they use almost no disk space). This is unlike real links, but those can't cross file system boundaries. In extreme cases the presence of very many symbolic links can lead to exhaustion of the number of foreseen files (so called inodes), even though there is plenty of space left. In this case the sysadmin will need to tune the file system. Not me, not you β€” the sysadmin πŸ’€

Overriding repository copies πŸ‘‘

If you make any modifications to a file locally, makepp will ordinarily realize this and recompile the file using the local copy rather than the repository copy.

If you're using a repository to maintain a central code base, and you have developers working on local copies which contain only the files they have modified, one problem that comes up is: what if a developer wants to remove a file from his local build but the repository still contains it? If the developer removes the local copy, makepp will happily put in the copy from the repository, and the build will proceed as if the file existed. Rude, honestly 😭

One technique (alas not for user root) for this problem is to make the file that you want not to include in the build process unreadable, like this:

chmod a-rw file-to-be-excluded

This will prevent makepp from incorporating it from the repository. Makepp also includes special code so that unreadable files do not match wildcards or pattern rules. πŸ¦‹

Similarly, if you wanna stop makepp from swallowing a whole subdirectory whole, just make a local directory with the same name but make it unwritable. πŸš«πŸ“ And if you want makepp to ignore that directory entirely, then make it unreadable too. (Read-only directories still get searched, but targets inside them usually don't get built, iykyk.)

The other way to pull this off is by calling makepp with one or more exclusion options:

mpp -R /path/to/rep --dont-read=/path/to/rep/file-to-be-excluded

🚨 Don't use repositories for files which can change! 🚨

Bestie. Listen. Do not try to use a repository for a file that's part of your build. Like, I know the temptation is real β€” you might be like "omg what if I put all my public .h files in one directory using repositories", like this:

# top level makefile
repository include=module1/include
repository include=module2/include
repository include=module3/include
repository include=module4/include

Yeah… probably not a good idea if any of those .h files are themselves outputs of a program (think yacc, or literally any other program that spits out C source code), because makepp assumes files in repositories never change. πŸ’€ If your build needs include/xyz.h, and module2/include/xyz.h actually needs to be produced by some program, makepp will not know to run that program. No cap.

Instead, it's better to use a technique like this to put all your .h files into a common include directory:

# module1/Makeppfile
../include/%.h : include/%.h
    &cp $(input) $(output)
# You could also (more efficiently but problematic on Windows) do the following:
#    &ln -r $(input) $(output)

Makepp might still try to build files that happen to be in a repository if something asks for them directly, but it won't build them on behalf of the local directory. And the result of that can be quite confusing, because it can lead to a repository symbolic link being used while its repository target is out-of-date β€” but that target might get updated later in the build. 😭 The way to prevent this from happening is either:

  • making sure the repository is referred to only through the repository path, or
  • making sure there's also a local rule for all the generated repository files.

Another way to avoid recompiling identical files in different directories is to use a build cache (see makepp_build_cache for details). And the iconic part? A build cache does not have the restriction that the file may not change. ✨ Periodt.


That's the whole page, bestie! πŸŽ€ This was MAKEPP_REPOSITORIES(1), from perl v5.32.0, dated 2021-01-06. Go build something iconic. πŸ¦‹πŸ’–

πŸ“¦ where this page comes from