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

Fix Makefile escaping #607

Merged
merged 2 commits into from
Feb 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ ifeq ($(OS),lnx)
MKDIR := mkdir -p
OBJCOPY := objcopy
FIXPATH = $1
ESCAPENAME = $(subst ','\'',$1)
ADDQUOTES = '$1'
PLATFORM := posix
EXTENSION :=

Expand Down Expand Up @@ -72,6 +74,8 @@ ifeq ($(OS),win)
RM := del /Q
MKDIR := mkdir
FIXPATH = $(subst /,\,$1)
ESCAPENAME = $1
ADDQUOTES = "$1"
PLATFORM := windows
EXTENSION := .exe

Expand All @@ -96,6 +100,8 @@ ifeq ($(OS),osx)
RM := rm -fr
MKDIR := mkdir -p
FIXPATH = $1
ESCAPENAME = $(subst ','\'',$1)
ADDQUOTES = '$1'
BITS := 64
PLATFORM := posix
EXTENSION :=
Expand Down Expand Up @@ -130,7 +136,8 @@ GENERATE_LICENSE ?= n
LICENSE ?= $(EXE).license.txt
LICENSE_IN_USE := qb64 tinyfiledialogs

all: $(EXE)
.PHONY: exe
all: exe

CLEAN_LIST :=
CLEAN_DEP_LIST :=
Expand Down Expand Up @@ -422,24 +429,28 @@ clean: $(CLEAN_DEP_LIST)

ifeq ($(GENERATE_LICENSE),y)
LICENSE_FILES := $(patsubst %,licenses/license_%.txt,$(LICENSE_IN_USE))
LICENSE_DEP := $(LICENSE)
LICENSE_DEP := license
endif

$(EXE): $(EXE_OBJS) $(EXE_LIBS) $(LICENSE_DEP)
$(CXX) $(CXXFLAGS) $(EXE_OBJS) -o "$@" $(EXE_LIBS) $(CXXLIBS)
# Special care has to be taken for EXE because it is the user-supplied
# executable name - it can contain any valid characters. We have to quote it
# when passing it on the command-line and additionally have to escape the quote
exe: $(EXE_OBJS) $(EXE_LIBS) $(LICENSE_DEP)
$(CXX) $(CXXFLAGS) $(EXE_OBJS) -o $(call ADDQUOTES,$(call ESCAPENAME,$(EXE))) $(EXE_LIBS) $(CXXLIBS)
ifneq ($(filter-out osx,$(OS)),)
ifneq ($(STRIP_SYMBOLS),n)
$(OBJCOPY) --only-keep-debug "$@" "$(PATH_INTERNAL_TEMP)/$(notdir $@).sym"
$(OBJCOPY) --strip-unneeded "$@"
$(OBJCOPY) --only-keep-debug $(call ADDQUOTES,$(call ESCAPENAME,$(EXE))) $(call ADDQUOTES,$(PATH_INTERNAL_TEMP)/$(notdir $(call ESCAPENAME,$(EXE))).sym)
$(OBJCOPY) --strip-unneeded $(call ADDQUOTES,$(call ESCAPENAME,$(EXE)))
endif
endif

$(LICENSE): $(LICENSE_FILES)
.PHONY: license
license: $(LICENSE_FILES)
ifeq ($(GENERATE_LICENSE),y)
ifeq ($(OS),win)
type $(call FIXPATH,$^) > "$@"
type $(call FIXPATH,$^) > $(call ADDQUOTES,$(call ESCAPENAME,$(LICENSE)))
else
cat $^ > "$@"
cat $^ > $(call ADDQUOTES,$(call ESCAPENAME,$(LICENSE)))
endif
endif

Expand Down
16 changes: 13 additions & 3 deletions source/qb64pe.bas
Original file line number Diff line number Diff line change
Expand Up @@ -12516,11 +12516,21 @@ CxxLibsExtra$ = CxxLibsExtra$ + " " + mylib$ + " " + mylibopt$

' Make and the shell don't like certain characters in the file name, so we
' escape them to get them to handle them properly
escapedExe$ = StrReplace$(path.exe$ + file$ + extension$, " ", "\ ")
escapedExe$ = StrReplace$(escapedExe$, CHR$(34), "\" + CHR$(34))
escapedExe$ = path.exe$ + file$ + extension$

' Make escaping
escapedExe$ = StrReplace$(escapedExe$, _CHR_QUOTE, "\" + _CHR_QUOTE)
escapedExe$ = StrReplace$(escapedExe$, "$", "$$")

makeline$ = make$ + makedeps$ + " EXE=" + AddQuotes$(escapedExe$)
IF os$ = "LNX" THEN
' sh escaping
escapedExe$ = StrReplace$(escapedExe$, "'", "'\''")
escapedExe$ = "'" + escapedExe$ + "'"
ELSE
escapedExe$ = _CHR_QUOTE + escapedExe$ + _CHR_QUOTE
END IF

makeline$ = make$ + makedeps$ + " EXE=" + escapedExe$
makeline$ = makeline$ + " " + AddQuotes$("CXXFLAGS_EXTRA=" + CxxFlagsExtra$)
makeline$ = makeline$ + " " + AddQuotes$("CFLAGS_EXTRA=" + CxxFlagsExtra$)
makeline$ = makeline$ + " " + AddQuotes$("CXXLIBS_EXTRA=" + CxxLibsExtra$)
Expand Down
3 changes: 3 additions & 0 deletions tests/compile_tests/dollar$sign$test/dollar$sign.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$console:only
Print "Hello, World"
System
1 change: 1 addition & 0 deletions tests/compile_tests/dollar$sign$test/dollar$sign.output
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, World
3 changes: 3 additions & 0 deletions tests/compile_tests/parens()/parens().bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$Console:Only
Print "Hello, World"
System
1 change: 1 addition & 0 deletions tests/compile_tests/parens()/parens().output
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, World
Loading