Last active
March 10, 2025 15:55
-
-
Save sleeptightAnsiC/09216c2ab63a944a5a6508c9d0298e22 to your computer and use it in GitHub Desktop.
Shell script for renaming symbols in Raylib's source tree.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/sh | |
set -e | |
# | |
# Copyright (c) 2025 sleeptightAnsiC https://github.com/sleeptightAnsiC | |
# YOU ARE PERMITED TO COPY, USE AND EDIT THIS SCRIPT LOCALLY, | |
# BUT DO NOT REDISTRIBUTE IT WITHOUT PRIOR NOTICING ME, PLEASE! | |
# THIS SCRIPT IS NOT FULLY TESTED! YOU ARE USING IT ON YOUR OWN RISK! | |
# | |
# This script prefixes every name used by Raylib in order to prevent namespace clashing. | |
# Treat it as a proof of concept or MVP. It probably should be rewritten in C someday. | |
# Depends on bourne shell, make, wc, sed, and grep. I tried to keep it POSIX compliant. | |
# It also depends on 'raylib/parser/raylib_parser.c' in order to pull symbol names. | |
# You should be able to run it on MacOSX, most Linux distros, most BSDs and through BusyBox. | |
# On Windows, you can use something like w64devkit: https://github.com/skeeto/w64devkit | |
# | |
# WARN: By default, uses prefixes: 'R' for symbols; 'R_' for macros and constants. | |
# WARN: Not fully tested! probably probably has few bugs ! | |
# WARN: It may not work if re-run on already renamed sources ! | |
# WARN: Super slow... Takes around 40sec on x86_64 Linux with 6 core i5-8600k | |
# | |
# In order to use: | |
# $ git clone https://github.com/raysan5/raylib.git | |
# $ cd raylib/parser | |
# (paste the script into this directory and name it raylib_renamer.sh ) | |
# $ sh ./raylib_renamer.sh | |
# (names in library and examples should be changed, you can compile now) | |
if ! ls ./raylib_parser.c ./Makefile > /dev/null; then | |
echo "Error: You need to run this script from 'raylib/parser' directory!" | |
exit 1 | |
fi; | |
# These are prefixes that will be used | |
# WARN: do not use 'rl', 'RL', 'RL_' because 'raylib/src/rlgl.h' already uses them | |
# WARN: You can use 'RAY' and 'Ray', but keep in mind there is one struct called "Ray" | |
PREFIX_MACRO="R_" | |
PREFIX_SYMBOL="R" | |
COMMANDS_FILE="./raylib_renamer_commands.sed" | |
# # Only for debugging purposes: reverts all changes applied by script | |
# git restore ../src ../examples/ | |
# rm -fr ../build ./raylib_api.txt ./raylib_parser $COMMANDS_FILE | |
# TODO: I wonder, if I can reimplement this with AWK ? | |
make raylib_api.txt | |
SYMBOLS=$(sed < raylib_api.txt -e ' | |
s/^ Name: //; | |
s/^ Value\[//; | |
s/[^A-Za-z0-9_ ].*$//; | |
/ /d; | |
/^RAYLIB/d; | |
/^RL/d; | |
/^__/d; | |
') | |
# echo $SYMBOLS | |
# exit 1 | |
echo "Generating sed commands in $COMMANDS_FILE ..." | |
echo "" > $COMMANDS_FILE | |
# This is a sed regex capture for everything that cannot be in C symbol/macro name | |
NOT_CNAME="\([^A-Za-z0-9_]\+\)" | |
for i in $SYMBOLS; do | |
# TODO: this should avoid using 'grep' so we have one less dependency | |
if echo "$i" | grep -E '^ ?[^a-z]*$' > /dev/null; then | |
NEWSYM="$PREFIX_MACRO$i" | |
else | |
NEWSYM="$PREFIX_SYMBOL$i" | |
fi; | |
# we want to use 'echo', not 'printf' here (ignore shellcheck's SC2028 warning) | |
echo "s/^$i$NOT_CNAME/$NEWSYM\1/g;" >> $COMMANDS_FILE | |
echo "s/$NOT_CNAME$i\$/\1$NEWSYM/g;" >> $COMMANDS_FILE | |
# FIXME: for some reason this needs to be used twice for some files (?) | |
echo "s/$NOT_CNAME$i$NOT_CNAME/\1$NEWSYM\2/g;" >> $COMMANDS_FILE | |
echo "s/$NOT_CNAME$i$NOT_CNAME/\1$NEWSYM\2/g;" >> $COMMANDS_FILE | |
done | |
# cat $COMMANDS_FILE | |
# exit 1 | |
# We need to check because `nproc` is not POSIX | |
if command -v nproc > /dev/null; then | |
JOBS=$(nproc) | |
else | |
echo "Warning: 'nproc' command is missing. Only 2 threads will be used." | |
JOBS=2 | |
fi; | |
rename_in_file () { | |
set -e | |
file="$1" | |
echo "Renaming in: '$file' ..." | |
# WARN: we need to use .tmp file because POSIX sed does not support '-i' | |
# and '-i' pretty much does the same thing but worse... | |
sed -f "$COMMANDS_FILE" "$file" > "$file.tmp" | |
mv -f "$file.tmp" "$file" | |
} | |
FILES=$(echo \ | |
../src/*.c \ | |
../src/*.h \ | |
../src/platforms/*.c \ | |
../src/external/rl_gputex.h \ | |
../src/external/win32_clipboard.h \ | |
../examples/*/*.c \ | |
../examples/*/*.h \ | |
) | |
# echo $FILES | |
# exit 1 | |
for file in $FILES; do | |
rename_in_file "$file" & | |
while [ "$(jobs | wc -l)" -ge "$JOBS" ]; do | |
# I would preffer 'wait -n' here but it's bash-specific | |
sleep 0.5 | |
done | |
done | |
# # Only for debugging purposes: build examples with cmake in order to test everything | |
# mkdir ../build | |
# cd ../build | |
# cmake -G "Unix Makefiles" .. | |
# # WARN: this fails for examples/shapes/shapes_draw_*.c on my machine due to: | |
# # https://github.com/raysan5/raylib/issues/4820 | |
# make -j"$(nproc)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Notes: