Skip to content

Commit

Permalink
platform-agnostic makefiles
Browse files Browse the repository at this point in the history
  • Loading branch information
vmchale committed Jul 18, 2024
1 parent 886926b commit f4faa62
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
14 changes: 12 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
include mk/os.mk

HC ?= ghc

HS_SRC := $(shell find src -type f) $(shell find lib -type f) apple.cabal
ifeq ($(UNAME),Linux)
LD_VER := $(shell ja '{%/^\s*lib-version-info:/}{`2}' -i apple.cabal | sed 's/:/./g')
endif

libapple.dylib: $(HS_SRC) include/apple.h
libapple$(EXT): $(HS_SRC) include/apple.h
cabal build flib:apple -w $(HC)
ifeq ($(UNAME),Linux)
cp $$(cabal-plan list-bins apple:flib:apple | awk '{print $$2}').$(LD_VER) $@
strip $@
else
cp $$(cabal-plan list-bins apple:flib:apple | awk '{print $$2}') $@
endif

moddeps.svg: $(HS_SRC)
graphmod -i src | dot -Tsvg -o $@

install-lib: libapple.dylib
install-lib: libapple$(EXT)
cp $^ /usr/local/lib

install-py:
Expand Down
23 changes: 16 additions & 7 deletions Rc/Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
HC ?= ghc

include ../mk/os.mk

GHC_VER := $(shell ${HC} --numeric-version)
R_VER := $(shell R --version | ja '.?{ix=1}{`0 ~* 1 /(\d+\.\d+)/}')
HS_LIBDIR := $(shell ghc-pkg-$(GHC_VER) field rts dynamic-library-dirs | ja '{ix=1}{`2}')
HS_INCLUDE_DIR := $(shell ghc-pkg-$(GHC_VER) field rts include-dirs | ja '{ix=2}{`1}')

all: libappler.dylib
ifeq ($(UNAME),Darwin)
HS_INCLUDE_DIR := $(shell ghc-pkg-$(GHC_VER) field rts include-dirs | ja '{ix=2}{`1}')
CFLAGS := -I /Library/Frameworks/R.framework/Versions/$(R_VER)-arm64/Headers
LDFLAGS := -L /usr/local/lib -rpath /usr/local/lib -lHSrts-1.0.2-ghc$(GHC_VER) -L $(HS_LIBDIR) -rpath $(HS_LIBDIR) /Library/Frameworks/R.framework/Versions/$(R_VER)-arm64/R
else
HS_INCLUDE_DIR := $(shell ghc-pkg-$(GHC_VER) field rts include-dirs | ja '{ix=1}{`2}')
endif

all: libappler$(EXT)

install: libappler.dylib
install: libappler$(EXT)
cp $^ /usr/local/lib

%.o: %.c
gcc -fPIC -O2 -c $< -I /Library/Frameworks/R.framework/Versions/$(R_VER)-arm64/Headers -I $(HS_INCLUDE_DIR) -o $@
$(CC) -fPIC -O2 -c $< $(CFLAGS) -I $(HS_INCLUDE_DIR) -o $@

libappler.dylib: appler.o
gcc -shared $^ -o $@ -lapple -lffi -L /usr/local/lib -rpath /usr/local/lib -lHSrts-1.0.2-ghc$(GHC_VER) -L $(HS_LIBDIR) -rpath $(HS_LIBDIR) /Library/Frameworks/R.framework/Versions/$(R_VER)-arm64/R
libappler$(EXT): appler.o
$(CC) -shared $^ -o $@ -lapple -lffi $(LDFLAGS)

clean:
rm -rf *.o *.dylib
rm -rf *.o *.so *.dylib
7 changes: 7 additions & 0 deletions mk/os.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
UNAME := $(shell uname)

ifeq ($(UNAME),Darwin)
EXT := .dylib
else
EXT := .so
endif
21 changes: 18 additions & 3 deletions pyc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@ HC ?= ghc

GHC_VER := $(shell ${HC} --numeric-version)
PY_VER := $(shell python3 --version | ja '.?{|`2 ~* 1 /(\d+\.\d+)\.\d+/}')

UNAME := $(shell uname)

ifeq ($(UNAME),Darwin)
CFLAGS=-I /Library/Frameworks/Python.framework/Versions/$(PY_VER)/include/python$(PY_VER) -I /Library/Frameworks/Python.framework/Versions/$(PY_VER)/lib/python$(PY_VER)/site-packages/numpy/_core/include
LDFLAGS=-L /usr/local/lib -rpath /usr/local/lib -lHSrts-1.0.2-ghc$(GHC_VER) -L $(HS_LIBDIR) -rpath $(HS_LIBDIR) /Library/Frameworks/Python.framework/Versions/$(PY_VER)/Python
else
CFLAGS=-I "$$(python3 -m site --user-site)/numpy/_core/include" -I /usr/local/include/python$(PY_VER)

endif

HS_LIBDIR := $(shell ghc-pkg-$(GHC_VER) field rts dynamic-library-dirs | ja '{ix=1}{`2}')
HS_INCLUDE_DIR := $(shell ghc-pkg-$(GHC_VER) field rts include-dirs | ja '{ix=2}{`1}')
ifeq ($(UNAME),Darwin)
HS_INCLUDE_DIR := $(shell ghc-pkg-$(GHC_VER) field rts include-dirs | ja '{ix=2}{`1}')
else
HS_INCLUDE_DIR := $(shell ghc-pkg-$(GHC_VER) field rts include-dirs | ja '{ix=1}{`2}')
endif

all: apple.so

Expand All @@ -13,10 +28,10 @@ install: apple.so
# cdeps applemodule.c | ja "[x+' '+y]|> \$0"

apple.o: applemodule.c
$(CC) -fPIC -O2 -c $< -I /Library/Frameworks/Python.framework/Versions/$(PY_VER)/include/python$(PY_VER) -I /Library/Frameworks/Python.framework/Versions/$(PY_VER)/lib/python$(PY_VER)/site-packages/numpy/_core/include -I ../include -I $(HS_INCLUDE_DIR) -o $@
$(CC) -fPIC -O2 -c $< $(CFLAGS) -I ../include -I $(HS_INCLUDE_DIR) -o $@

apple.so: apple.o
$(CC) -shared $^ -o $@ -lapple -lffi -L /usr/local/lib -rpath /usr/local/lib -lHSrts-1.0.2-ghc$(GHC_VER) -L $(HS_LIBDIR) -rpath $(HS_LIBDIR) /Library/Frameworks/Python.framework/Versions/$(PY_VER)/Python
$(CC) -shared $^ -o $@ -lapple -lffi $(LDFLAGS)

clean:
rm -rf *.o *.so

0 comments on commit f4faa62

Please sign in to comment.