This script automates the process of updating the Sources.mk
file by scanning a source directory for .c
files (and .m
files on macOS) and appending their paths under a specified variable.
- Supports both Linux and macOS.
- Scans the specified source directory for
.c
(and.m
files on macOS). - Automatically updates or creates the variable
SRC_FILES :=
in theSources.mk
file. - Uses color-coded output for better readability of errors and success messages.
Make sure the following tools are installed and accessible in your environment:
bash
(this script uses Bash syntax)find
(to locate source files)sed
(to edit theSources.mk
file)
The script uses the following default variables:
SRC_DIR
: The directory containing the source files (default:./src
)SRC_MK
: The targetSources.mk
file (default:Sources.mk
)VAR_NAME
: The variable to update in theSources.mk
file (default:SRC_FILES :=
)
To run the script, execute:
./update_sources.sh
- If the
SRC_DIR
does not exist, the script will display an error and exit. - If no
.c
files are found in theSRC_DIR
, the script will display an error and exit. - If
Sources.mk
exists, the script will replace theSRC_FILES :=
section with the updated list of source files. - If
Sources.mk
does not exist, the script will display an error and exit.
-
Success Message:
The file 'Sources.mk' has been successfully updated.
-
Error Message:
Error: The source directory './src' does not exist.
This script is compatible with:
- macOS
- Linux
Note: On macOS, the sed
command uses the -i ""
flag for in-place editing.
You can modify the script to:
- Change the source directory (
SRC_DIR
) - Target a different makefile (
SRC_MK
) - Use a different variable name (
VAR_NAME
)
For example, to update a different source directory, set SRC_DIR
:
SRC_DIR="./custom_source" ./update_sources.sh
- Issue:
Sources.mk
is not updated.- Ensure the
SRC_MK
file exists in the current working directory. - Verify that there are
.c
files in the specifiedSRC_DIR
.
- Ensure the
- Issue: Error messages about missing tools.
- Check that
find
andsed
are installed and accessible in your shell environment.
- Check that
Feel free to use and adapt this script for your projects.