Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build improvements #114

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open

Conversation

lorinder
Copy link
Contributor

Implement a number of improvements to the build system

  • Use explicit rather than phony targets, so make can track dependencies.
  • Implement ./configure for easier integration into packaging scripts and build environments
  • Use standardized Makefile variables like CC, CFLAGS, RM, etc.
  • No implicit root

lorinder and others added 17 commits December 22, 2024 11:31
Among other things, this allows `make` to correctly track dependencies,
i.e., what needs to be rebuilt when changes to source are made.
The login shell can be anything.  It may not be a POSIX shell (e.g., it
could be fish) in which case the build would break.  Or it could be zsh,
which would make the build unnecessarily slow.

Default to `sh` instead which is a posix shell.  Add Makefile variable
BUILD_SHELL to make this configurable.
Avoids hard coding the compiler.  Allows to pick a custom compiler using
the standard `CC=... make` approach.
This is useful since that is a standardized flag, and many build
environments set it upon invoking make to the desired values.
Should not elevate privileges for install, as this avoids the rule from
working in non-interactive uses (e.g. packaging scripts).  Also, with
upcoming changes such as support for setting the install prefix, root
privileges are not always needed.  Finally, it's better for users to
explicitly "sudo make install", rather than risking unintentional
elevation of privileges.
Support for the quasi-standard "./configure && make && make install"
install command sequence.  The "configure" part remains optional, i.e.,
"make && make install" still works, though behind the scenes it does
invoke configure.

So far, we only support the fairly minimal "--prefix" flag to configure,
which is used by packaging scripts to set the installation location.
`mkdir build` is already run by the configure script;  thus the
individual build rules no longer need to perform this test.
Add dependencies that I had missed.  This set is complete, as it was
derived by running "gcc -MM".
This is one of the C11 features that are required by TCC.
Copy link
Collaborator

@laurenthuberdeau laurenthuberdeau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the much needed improvement to the Makefile! I found 2 small issues and left some questions on parts of the Makefile I wasn't too familiar with.

sudo cp $(BUILD_DIR)/pnut-sh.sh /usr/local/bin/pnut.sh
install: $(BUILD_DIR)/pnut-sh $(BUILD_DIR)/pnut-sh.sh $(BUILD_DIR)/config.mk
install -D $(BUILD_DIR)/pnut-sh $(DESTDIR)$(prefix)/bin/pnut
install -D $(BUILD_DIR)/pnut-sh.sh $(DESTDIR)$(prefix)/bin/pnut.sh
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like install -D is not BSD-compatible and so make install won't work on macOS 😕

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, good catch. I didn't know this.

$(BUILD_DIR)/config.mk:
./configure

include $(BUILD_DIR)/config.mk
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

./configure scripts are usually called before using make but it looks like the Makefile creates it if it's missing (but only when make install is used?) so I'm not sure what's the expected usage?

Also, when calling make without first calling the configure script, it complains with Makefile:38: build/config.mk: No such file or directory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. So the idea was to add support for the ./configure && make approach to building, but keep things working for those that just type make, without the configure first. In order to support that second approach, I added the rule to the Makefile to create config.mk if it does not exist.

You're right that make complains, about the missing config.mk, but then proceeds to create it and load it and all is fine, i.e., the error message is spurious. The GNU make manual contains the following section explaining this:

If an included makefile cannot be found in any of these directories it is not an immediately fatal error; processing of the makefile containing the include continues. Once it has finished reading makefiles, make will try to remake any that are out of date or don’t exist. See How Makefiles Are Remade. Only after it has failed to find a rule to remake the makefile, or it found a rule but the recipe failed, will make diagnose the missing makefile as a fatal error.

sudo cp $(BUILD_DIR)/pnut-sh /usr/local/bin/pnut
sudo cp $(BUILD_DIR)/pnut-sh.sh /usr/local/bin/pnut.sh
install: $(BUILD_DIR)/pnut-sh $(BUILD_DIR)/pnut-sh.sh $(BUILD_DIR)/config.mk
install -D $(BUILD_DIR)/pnut-sh $(DESTDIR)$(prefix)/bin/pnut
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how prefix is intended to be used. Can't we simply override DESTDIR instead?

Copy link
Contributor Author

@lorinder lorinder Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are used for different purposes. I recommend to look at this explanation; it's a well written description, in my opinion.

include $(BUILD_DIR)/config.mk

PNUT_SH_DEPS=pnut.c debug.c sh.c sh-runtime.c env.c
PNUT_EXE_DEPS= pnut.c debug.c x86.c exe.c env.c elf.c mach-o.c
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know about gcc -MM. It would have been useful when implementing the measure-file-size.sh script!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants