Skip to content

Commit

Permalink
add rules for building dmg
Browse files Browse the repository at this point in the history
  • Loading branch information
CamJN committed Nov 30, 2023
1 parent 1aa13e8 commit 03d1b27
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ compile_commands.json
/test/share
default.profraw
/test/include
/pkg
/fakeroot
84 changes: 43 additions & 41 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,46 +1,12 @@
LIBVER ?=$(shell vtool -show-build $(shell brew --prefix)/lib/libgetargv.dylib | awk '/minos/{print $$2}')
export MACOSX_DEPLOYMENT_TARGET=$(LIBVER)
VERSION=0.1
COMPAT_VERSION := $(shell echo $(VERSION) | cut -f1 -d.).0

CODESIGN_PREFIX := cam.narzt.
KEYCHAIN := ~/Library/Keychains/login.keychain-db
CERT_IDENTITY := $(shell security find-identity -v -p codesigning | sed -Ee 's/.*"([^"]+)".*/\1/g' | grep -Fve ' valid identit' -e ' CA')

SRC_DIR = src
OBJ_DIR = obj
LIB_DIR = lib

PREFIX := /usr/local
CXX=clang++
CPPFLAGS += -MMD -MP

COMPILER_VERSION := $(shell $(CXX) --version | grep version | grep -o -m 1 "[0-9]\+\.[0-9]\+\.*[0-9]*" | head -n 1)
COMPILER_VERSION_NUMBER := $(shell echo $(COMPILER_VERSION) | sed -e 's/\.\([0-9][0-9]\)/\1/g' -e 's/\.\([0-9]\)/0\1/g' -e 's/^[0-9]\{3,4\}$$/&00/')
CLANG_13_OR_MORE := $(shell expr $(COMPILER_VERSION_NUMBER) \>= 130106)
ifneq ($(CLANG_13_OR_MORE),0)
# supported: c++11, c++14, c++17, c++20
# future: c++2b
CXXFLAGS := --std=c++20 -O3 -Iinclude
else
CXXFLAGS := --std=c++17 -O3 -Iinclude
endif

EXTRA_CXXFLAGS := -pedantic-errors -Weverything -Wno-c++98-compat -Wno-pre-c++20-compat-pedantic -Wno-poison-system-directories
LDFLAGS += -Llib -fvisibility=default -fPIC
LDLIBS += -lgetargv

LIB_SHORT_NAME = getargv++
DYLIB_FILENAME = lib$(LIB_SHORT_NAME).$(VERSION).dylib
DYLIB = lib/$(DYLIB_FILENAME)
SOURCES = $(wildcard $(SRC_DIR)/*.cpp)
OBJECTS = $(SOURCES:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.o)
include Makefile-variables

.PHONY := db clean dylib install_dylib lint
.PHONY := db clean dylib install install_dylib lint dmg
.DEFAULT_GOAL := dylib

dylib: $(DYLIB)

install: install_dylib

install_dylib: $(DYLIB)
install -d $(PREFIX)/$(LIB_DIR)
install $(DYLIB) $(PREFIX)/$(DYLIB)
Expand All @@ -62,10 +28,10 @@ $(DYLIB): $(OBJECTS) | $(LIB_DIR)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | $(OBJ_DIR)
$(CXX) $(EXTRA_CXXFLAGS) $(CXXFLAGS) $(CPPFLAGS) -c -fPIC $< -o $@

$(OBJ_DIR) $(LIB_DIR):
$(OBJ_DIR) $(LIB_DIR) $(FAKE_ROOT) $(PKG_DIR):
mkdir -p $@

docs:
docs: doxygen.conf $(SOURCES) $(HEADERS)
doxygen -q doxygen.conf

db: compile_commands.json
Expand All @@ -74,10 +40,46 @@ compile_commands.json: Makefile
$(shell brew --prefix bear)/bin/bear -- make -B $(OBJECTS)

clean:
@$(RM) -rf $(OBJ_DIR) $(LIB_DIR) docs
@$(RM) -rf $(OBJ_DIR) $(LIB_DIR) docs $(FAKE_ROOT) $(PKG_DIR)

lint: compile_commands.json
for file in $(SOURCES); do /usr/bin/xcrun -r clangd --enable-config --clang-tidy --log=error --check=$$file; done
$(shell brew --prefix llvm)/bin/scan-build -enable-checker security -enable-checker unix -enable-checker valist $(MAKE) -B dylib

dmg: $(DMG)

$(DMG): $(PRODUCT)
hdiutil create -fs "$(DMG_FS)" -volname "$(DMG_VOLUME_NAME)" -srcfolder "$(PKG_DIR)" -ov -format "$(DMG_FORMAT)" "$@"

$(DISTRIBUTION): $(SRC_DIR)/dist.xml
< $< > $@ sed \
-e 's/OS_VERSION/$(MACOS_VER_NUM)/g' \
-e 's/ARCH/$(ARCH)/g' \
-e 's/VERSION/$(VERSION)/g' \
-e 's/LIB_PKG_NAME/$(LIB_PKG:$(PKG_DIR)/%=%)/g' \
-e 's/LIB_ID/$(LIB_BUNDLE_IDENTIFIER)/g' \
-e 's/LIB_NAME/$(LIB_NAME)/g'

$(PRODUCT): $(LIB_PKG) $(DISTRIBUTION)
productbuild \
--identifier $(PRODUCT_BUNDLE_IDENTIFIER) \
--version $(VERSION) \
--package-path $(PKG_DIR) \
--resources ./ \
--distribution $(DISTRIBUTION) \
$(SIGN_PACKAGE_FLAG) \
$@
@$(RM) $^

$(LIB_PKG): $(FAKE_ROOT) | $(PKG_DIR)
@$(RM) -rf $(FAKE_ROOT)/*
$(MAKE) PREFIX=$(FAKE_ROOT) install_dylib
pkgbuild --root $(FAKE_ROOT) \
--identifier "$(LIB_BUNDLE_IDENTIFIER)" \
--version "$(VERSION)" \
$(PKG_VERSION_FLAG) \
--install-location "$(PREFIX)" \
$(SIGN_PACKAGE_FLAG) \
$@

-include $(OBJECTS:.o=.d)
75 changes: 75 additions & 0 deletions Makefile-variables
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
MACOS_VER_NUM ?=$(shell vtool -show-build $(shell brew --prefix)/lib/libgetargv.dylib | awk '/minos/{print $$2}')
MACOS_VER_MAJOR := $(shell echo $(MACOS_VER_NUM) | cut -f1 -d.)
MACOS_VER_MINOR := $(shell echo $(MACOS_VER_NUM) | cut -f2 -d.)
MACOS_LT_10_11 := $(shell [ $(MACOS_VER_MAJOR) -eq 10 -a $(MACOS_VER_MINOR) -lt 11 ] && echo true)
MACOS_LT_10_13 := $(shell [ $(MACOS_VER_MAJOR) -eq 10 -a $(MACOS_VER_MINOR) -lt 13 ] && echo true)
MACOS_LT_11 := $(shell [ $(MACOS_VER_MAJOR) -eq 10 -a $(MACOS_VER_MINOR) -lt 15 ] || [ $(MACOS_VER_MAJOR) -lt 11 ] && echo true)
export MACOSX_DEPLOYMENT_TARGET := $(MACOS_VER_MAJOR).$(MACOS_VER_MINOR)
VERSION=0.1
COMPAT_VERSION := $(shell echo $(VERSION) | cut -f1 -d.).0
ARCH := $(shell uname -m)
CODESIGN_PREFIX := cam.narzt.
KEYCHAIN := ~/Library/Keychains/login.keychain-db
CERT_IDENTITY := $(shell security find-identity -v -p codesigning | sed -Ee 's/.*"([^"]+)".*/\1/g' | grep -Fve ' valid identit' -e ' CA')

SRC_DIR = src
INCLUDE_DIR = include
OBJ_DIR = obj
LIB_DIR = lib
PKG_DIR = pkg
FAKE_ROOT = fakeroot
PREFIX ?= /usr/local
CXX = clang++
CPPFLAGS += -MMD -MP

COMPILER_VERSION := $(shell $(CXX) --version | grep version | grep -o -m 1 "[0-9]\+\.[0-9]\+\.*[0-9]*" | head -n 1)
COMPILER_VERSION_NUMBER := $(shell echo $(COMPILER_VERSION) | sed -e 's/\.\([0-9][0-9]\)/\1/g' -e 's/\.\([0-9]\)/0\1/g' -e 's/^[0-9]\{3,4\}$$/&00/')
CLANG_13_OR_MORE := $(shell expr $(COMPILER_VERSION_NUMBER) \>= 130106)
ifneq ($(CLANG_13_OR_MORE),0)
# supported: c++11, c++14, c++17, c++20
# future: c++2b
CXXFLAGS := --std=c++20 -O3 -Iinclude
else
CXXFLAGS := --std=c++17 -O3 -Iinclude
endif

EXTRA_CXXFLAGS := -pedantic-errors -Weverything -Wno-c++98-compat -Wno-pre-c++20-compat-pedantic -Wno-poison-system-directories
LDFLAGS += -Llib -fvisibility=default -fPIC
LDLIBS += -lgetargv

LIB_SHORT_NAME = getargv++
LIB_NAME = lib$(LIB_SHORT_NAME)
DYLIB_FILENAME = $(LIB_NAME).$(VERSION).dylib
DYLIB = lib/$(DYLIB_FILENAME)
SOURCES = $(wildcard $(SRC_DIR)/*.cpp)
HEADERS = $(wildcard $(INCLUDE_DIR)/*.hpp)
OBJECTS = $(SOURCES:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.o)

DMG_VOLUME_NAME := $(LIB_NAME) Installer
DMG := $(PKG_DIR)/$(LIB_NAME).dmg
PRODUCT_BUNDLE_PACKAGE_TYPE := APPL
LIB_BUNDLE_IDENTIFIER := $(CODESIGN_PREFIX)$(LIB_NAME)
PRODUCT_BUNDLE_IDENTIFIER := $(LIB_BUNDLE_IDENTIFIER)
PRODUCT := $(PKG_DIR)/$(LIB_NAME)-$(VERSION)-macOS-$(MACOS_VER_NUM)-$(ARCH).pkg
LIB_PKG := $(PKG_DIR)/$(LIB_NAME)-$(VERSION)-macOS-$(MACOS_VER_NUM)-$(ARCH)-package.pkg
DISTRIBUTION := $(OBJ_DIR)/dist.xml

ifeq ($(MACOS_LT_10_11),true)
DMG_FORMAT := UDZO
else ifeq ($(MACOS_LT_11),true)
DMG_FORMAT := ULFO
else
DMG_FORMAT := ULMO
endif

ifeq ($(MACOS_LT_10_13),true)
DMG_FS := HFS+
else
DMG_FS := APFS
endif

ifeq ($(MACOS_LT_12),true)
PKG_VERSION_FLAG :=
else
PKG_VERSION_FLAG := --min-os-version $(MACOSX_DEPLOYMENT_TARGET)
endif
20 changes: 20 additions & 0 deletions src/dist.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<installer-gui-script minSpecVersion="2">
<volume-check>
<allowed-os-versions>
<os-version min="OS_VERSION" />
</allowed-os-versions>
</volume-check>

<license file="LICENSE" mime-type="text/plain" />

<options customize="allow" require-scripts="false" hostArchitectures="ARCH" />

<choices-outline>
<line choice="LIB_ID" />
</choices-outline>
<choice title="LIB_NAME library" id="LIB_ID" customLocation="/usr/local" customLocationAllowAlternateVolumes="true">
<pkg-ref id="LIB_ID" />
</choice>
<pkg-ref id="LIB_ID" version="VERSION" onConclusion="none">LIB_PKG_NAME</pkg-ref>
</installer-gui-script>

0 comments on commit 03d1b27

Please sign in to comment.