Skip to content

Commit

Permalink
Allow building using native GNU make
Browse files Browse the repository at this point in the history
Avoid the use of sed when building Compat.ml and use the NUL device.
  • Loading branch information
dra27 committed Apr 25, 2023
1 parent 56fdf1f commit 01a46f6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

msvs-detect text eol=lf
checkenv text eol=lf
Compat.cmd text eol=crlf
29 changes: 29 additions & 0 deletions Compat.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@setlocal
@echo off

:: This can be done as (slightly long) one-liner, but embedded in Makefile it
:: encodes the knowledge that GNU make uses batch files internally, which is a
:: bit brittle. findstr /N is a trick to allow blank lines to be included. More
:: subtly, it allows tagged blank lines to be detected.
for /f "tokens=1* delims=:" %%a in ('findstr /N /R "^" %2') do (
rem Handle blank lines in the file
if "%%b" equ "" (
echo.
) else (
for /f "tokens=1* delims=@" %%l in ("%%b") do (
rem If %%l == %%b then the line didn't contain @ - we already know the
rem line isn't blank.
if "%%l" equ "%%b" (
echo %%l
) else (
if %1 lss %%l00 (
if "%%m" equ "" (
echo.
) else (
echo %%m
)
)
)
)
)
)
30 changes: 25 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,28 @@
VERSION = 0.42
all: flexlink.exe support

OCAML_CONFIG_FILE=$(shell cygpath -ad "$(shell ocamlopt -where 2>/dev/null)/Makefile.config" 2>/dev/null)
export override OCAML_DETECT_CMD = detect
SHELL_IS_CMD := \
$(if $(filter %OCAML_DETECT_CMD%, $(shell echo %OCAML_DETECT_CMD%)),,true)
unexport OCAML_DETECT_CMD
undefine DETECT_WINDOWS_SHELL

NULL_DEVICE := $(if $(SHELL_IS_CMD), NUL, /dev/null)

ifeq ($(SHELL_IS_CMD),)
RM_F = rm -f $(1)
TOUCH = touch
else
RM_F = $(if $(wildcard $(1)),del /f $(subst /,\,$(wildcard ($1))))
TOUCH = type NUL >
endif

OCAML_CONFIG_FILE=$(shell cygpath -ad "$(shell ocamlopt -where 2>$(NULL_DEVICE))/Makefile.config" 2>$(NULL_DEVICE))
include $(OCAML_CONFIG_FILE)
OCAMLOPT=ocamlopt
EMPTY=
SPACE=$(EMPTY) $(EMPTY)
OCAML_VERSION:=$(firstword $(subst ~, ,$(subst +, ,$(shell $(OCAMLOPT) -version 2>/dev/null))))
OCAML_VERSION:=$(firstword $(subst ~, ,$(subst +, ,$(shell $(OCAMLOPT) -version 2>$(NULL_DEVICE)))))
ifeq ($(OCAML_VERSION),)
OCAML_VERSION:=0
COMPAT_VERSION:=0
Expand Down Expand Up @@ -148,8 +164,8 @@ build_mingw64: flexdll_mingw64.o flexdll_initer_mingw64.o
OBJS = version.ml Compat.ml coff.ml cmdline.ml create_dll.ml reloc.ml

COMPILER-$(COMPAT_VERSION):
rm -f COMPILER-*
touch COMPILER-$(COMPAT_VERSION)
$(call RM_F, COMPILER-*)
$(TOUCH) COMPILER-$(COMPAT_VERSION)

test_ver = $(shell if [ $(COMPAT_VERSION) -ge $(1) ] ; then echo ge ; fi)

Expand All @@ -163,11 +179,15 @@ COMPAT_LEVEL = $(eval COMPAT_LEVEL := \
$$(if $$(call test_ver,40700),407)))$(COMPAT_LEVEL)

Compat.ml: Compat.ml.in COMPILER-$(COMPAT_VERSION)
ifeq ($(SHELL_IS_CMD),)
sed -e '$(if $(COMPAT_LEVEL),/^$(subst $(SPACE),@\|^,$(COMPAT_LEVEL))@/d;)s/^[0-9]*@//' $< > $@
else
Compat.cmd $(COMPAT_VERSION) $< > $@
endif

flexlink.exe: $(OBJS) $(RES)
@echo Building flexlink.exe with TOOLCHAIN=$(TOOLCHAIN) for OCaml $(OCAML_VERSION)
rm -f flexlink.exe
$(call RM_F, flexlink.exe)
$(RES_PREFIX) $(OCAMLOPT) -o flexlink.exe $(LINKFLAGS) $(OBJS)

version.res: version.rc
Expand Down

0 comments on commit 01a46f6

Please sign in to comment.