Skip to content

Instantly share code, notes, and snippets.

@gvanem
Last active January 3, 2024 11:25
Show Gist options
  • Save gvanem/eaa3c4cc150a250ebcda559799948440 to your computer and use it in GitHub Desktop.
Save gvanem/eaa3c4cc150a250ebcda559799948440 to your computer and use it in GitHub Desktop.
A GNU-makefile for LTui (MSCV+clang-cl). Ref: https://github.com/tboox/ltui/
#
# GNUmake Makefile for Ltui (MSVC + clang-cl).
# Ref:
# https://github.com/tboox/ltui.git
#
# G.Vanem <[email protected]>, 2022.
#
TODAY := $(shell date +%d-%B-%Y)
THIS_FILE := Makefile.Windows
PYTHON ?= py -3
empty :=
spaces := $(empty) $(empty)
MAKEFLAGS += --warn-undefined-variables
#
# Options and roots. Change to suite.
# LuaJit does not work. Crashes at runtime :-(
#
# A nice reference for building Lua:
# http://lua-users.org/wiki/BuildingLuaInWindowsForNewbies
#
USE_LUA_JIT ?= 0
ifeq ($(USE_LUA_JIT),1)
LUA_ROOT ?= f:/MinGW32/src/LUA/LuaJIT
LUA_LIB ?= $(LUA_ROOT)/src/lua51.lib
LUA_BIN ?= $(LUA_ROOT)/src/luajit.exe
else
LUA_ROOT ?= f:/MinGW32/src/LUA/Lua-5.4
LUA_LIB ?= $(LUA_ROOT)/src/lua-$(CPU).lib
LUA_BIN ?= $(LUA_ROOT)/src/lua.exe
endif
CURSES_ROOT = src/core/pdcurses
VPATH = $(CURSES_ROOT)/pdcurses \
$(CURSES_ROOT)/win32 \
src/core/lcurses
define Usage
Usage: "make -f $(THIS_FILE) [CC=cl | clang-cl] [all | clean | vclean | depend]")
Specify CC=cl - use MSVC
Specify CC=clang-cl - use clang-cl
endef
#
# What to build:
#
TARGETS = ltui.dll
OBJ_DIR = objects
export CL=
ifneq ($(CC),cl)
ifneq ($(CC),clang-cl)
$(error $(Usage))
endif
endif
CFLAGS = -nologo -MD -O2 -Zi -W3 -GF -Gy \
-D_CRT_SECURE_NO_WARNINGS \
-D_CRT_NONSTDC_NO_WARNINGS \
-DPDCURSES \
-DPDC_WIDE \
-I$(CURSES_ROOT) \
-I$(LUA_ROOT)/src
ifeq ($(USE_LUA_JIT),1)
CFLAGS += -DLUAJIT
else
#
# Otherwise, use 'Lua*.DLL'.
#
CFLAGS += -DLUA_LIB -DLUA_BUILD_AS_DLL
endif
LDFLAGS = -nologo -verbose -incremental:no \
-nodefaultlib:oldnames.lib \
-nodefaultlib:uuid.lib -map -debug
ifeq ($(CC),clang-cl)
CFLAGS += -Wno-unused-function
endif
ltui_SRC = $(addprefix $(CURSES_ROOT)/pdcurses/, \
addch.c \
addchstr.c \
addstr.c \
attr.c \
beep.c \
bkgd.c \
border.c \
clear.c \
color.c \
debug.c \
delch.c \
deleteln.c \
deprec.c \
getch.c \
getstr.c \
getyx.c \
inch.c \
inchstr.c \
initscr.c \
inopts.c \
insch.c \
insstr.c \
instr.c \
kernel.c \
keyname.c \
mouse.c \
move.c \
outopts.c \
overlay.c \
pad.c \
panel.c \
printw.c \
refresh.c \
scanw.c \
scroll.c \
scr_dump.c \
slk.c \
termattr.c \
terminfo.c \
touch.c \
util.c \
window.c)
ltui_SRC += $(addprefix $(CURSES_ROOT)/win32/, \
pdcclip.c \
pdcdisp.c \
pdcgetsc.c \
pdckbd.c \
pdcscrn.c \
pdcsetsc.c \
pdcutil.c)
ltui_SRC += src/core/lcurses/lcurses.c
ltui_OBJ = $(addprefix $(OBJ_DIR)/, \
$(notdir $(ltui_SRC:.c=.obj)))
all: $(OBJ_DIR) $(TARGETS) epilogue
epilogue:
$(call green_msg, Welcome to ltui (CC=$(CC)).)
$(call green_msg, Remember to add:)
$(call white_msg, $(spaces)LUA_PATH=$(realpath .)/?.lua;$(realpath .)/src/?.lua$(BRIGHT_GREEN))
$(call green_msg, and:)
$(call white_msg, $(spaces)LUA_CPATH=$(realpath .)/?.dll.)
$(call green_msg, before use.)
$(OBJ_DIR):
- mkdir $@
ltui.dll: $(ltui_OBJ) $(LUA_LIB) | check-for-unused-libs.py
$(call link_DLL, $@, $^ advapi32.lib user32.lib)
$(OBJ_DIR)/%.obj: %.c
$(CC) -c $(CFLAGS) -Fo./$@ $<
@echo
clean:
rm -f link.tmp link.args vc1*.pdb ltui.{exp,lib} check-for-unused-libs.py
rm -fr $(OBJ_DIR)
realclean vclean: clean
rm -f $(TARGETS:.dll=.{dll,pdb,map}) .depend.Windows
rm -fr .xmake build
check-for-unused-libs.py: $(THIS_FILE)
$(call green_msg, Generating $@)
$(file > $@,#!/usr/env/python)
$(file >> $@,if 1:)
$(file >> $@,$(check_for_unused_libs_PY))
#
# GNU-make macros:
#
# The following assumes you have a MSys/Cygwin 'echo.exe' program with colour support.
#
BRIGHT_GREEN = \e[1;32m
BRIGHT_WHITE = \e[1;37m
colour_msg = @echo -e "$(1)\e[0m"
green_msg = $(call colour_msg,$(BRIGHT_GREEN)$(strip $(1)))
white_msg = $(call colour_msg,$(BRIGHT_WHITE)$(1))
#
# Delete the import library. No need for it.
#
define link_DLL
$(call green_msg, Linking $(1))
$(call create_resp_file, link.args, $(LDFLAGS) -dll $(2))
link -out:$(strip $(1)) @link.args > link.tmp
@cat link.tmp >> $(1:.dll=.map)
@rm -f $(1:.dll=.exp) $(1:.dll=.lib)
@$(PYTHON) check-for-unused-libs.py link.tmp
endef
define create_resp_file
$(file > $(1))
$(foreach f, $(2), $(file >> $(1),$(strip $(f))) )
endef
define check_for_unused_libs_PY
import os, sys
map_file = sys.argv[1]
ignore_libs = [ ]
class State():
IDLE = 0
UNUSED = 1
class Colour():
RED = WHITE = RESET = ""
try:
from colorama import init, Fore, Style
init()
Colour.RED = Fore.RED + Style.BRIGHT
Colour.WHITE = Fore.WHITE + Style.BRIGHT
Colour.RESET = Style.RESET_ALL
except:
pass
def report (unused):
num = len(unused)
if num > 0:
print ("%s%d unused libraries in %s:%s" % (Colour.RED, num, map_file, Colour.RESET))
for u in sorted(unused):
print (" " + u)
print ("%sDone%s\n" % (Colour.WHITE, Colour.RESET))
def process_map (state):
unused_libs = []
with open (map_file, "rt") as f:
lines = f.readlines()
for l in lines:
l = l.strip()
if l == "Unused libraries:":
state = State.UNUSED
continue
if state == State.UNUSED:
if l == "":
break
if os.path.basename (l).lower() not in ignore_libs:
unused_libs.append (l)
return unused_libs
report (process_map(State.IDLE))
endef
#
# Dependencies:
#
DEP_CFLAGS = -MM $(filter -D% -I%, $(CFLAGS))
DEP_REPLACE = sed -e 's@\(.*\)\.o: @\n$$(OBJ_DIR)\/\1.obj: @' \
-e 's@$(LUA_ROOT)@$$(LUA_ROOT)@'
depend:
$(call green_msg, Generating .depend.Windows...)
@echo '# Generated file. DO NOT EDIT!' > .depend.Windows
gcc $(DEP_CFLAGS) $(ltui_SRC) | $(DEP_REPLACE) >> .depend.Windows
-include .depend.Windows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment