Files
harbour-core/config/common/zig.mk
Aleksander Czajczynski aba6779100 2025-10-18 23:15 UTC+0200 Aleksander Czajczynski (hb fki.pl)
+ config/bsd/zig-noauto.mk
  + config/bsd/zig.mk
  + config/common/zig-noauto.mk
  + config/common/zig.mk
  + config/darwin/zig-noauto.mk
  + config/darwin/zig.mk
  + config/linux/zig-noauto.mk
  + config/linux/zig.mk
  + config/win/zig-noauto.mk
  + config/win/zig.mk
  * config/global.mk
  * utils/hbmk2/hbmk2.prg
    ! typo fixed in Alpha target (this path was really untested)

    + added support for using Zig as LLVM C compiler frontend. Recent
      idea comes from Marcos Gambeta Harbour++ fork, though I haven't
      borrowed any code. Here the functionalty is complete, whole
      Harbour source tree can be compiled, together with shared libs.

      Zig has unique ability of managing cross-compiled builds, mostly
      without downloading anything else. For example you should be able
      to easily make a Linux+musl libc build of your Harbour application
      that will run on any distribution with Linux kernel >= 2.6.39 (untested).

      Zig is not auto-detected by the Harbour build process, HB_COMPILER=zig
      has to be defined. The only exception is that, hbmk2 on Windows can
      locate "zig.exe" while building final Harbour applications. Usually
      there are no conflicting tools on PATH on this platform.

      After downloading zig from https://ziglang.org/download/
      specific to your OS, assuming the tool being unpacked to
      /home/user/zig or C:\zig, usage is as follows:

      Linux/BSD native build:
      PATH=$PATH:/home/user/zig
      HB_COMPILER=zig make -j8

      Unix to Windows x64 cross:
      PATH=$PATH:/home/user/zig
      export HB_ZIG_TARGET=x86_64-windows
      # (or) export HB_ZIG_TARGET=x86_64-windows-gnu
      export HB_CPU=x86_64
      # have to build a Harbour for your host first, for a native hbmk2
      export HB_HOST_BIN=/home/user/harbour/bin/linux/zig
      export HB_PLATFORM=win
      export HB_BUILD_NAME=64cross
      export HB_COMPILER=zig
      make -j16

      Windows native build:
      PATH=C:\zig;%PATH%
      set HB_COMPILER=zig
      win-make -j8

      Windows to Windows-on-ARM cross:
      set HB_ZIG_TARGET=aarch64-windows
      set HB_CPU=arm64
      set HB_HOST_BIN=C:\harbour\bin\win\zig
      set HB_COMPILER=zig
      win-make -j8

      Windows to Linux ARM64:
      set HB_USER_CFLAGS=-fPIC
      set HB_ZIG_TARGET=aarch64-linux
      set HB_PLATFORM=linux
      set HB_CPU=arm64
      set HB_HOST_BIN=C:\harbour\bin\win\zig
      set HB_COMPILER=zig
      win-make -j8

      Instead of setting HB_ZIG_TARGET, the less convenient way is:
      HB_USER_CFLAGS=-target aarch64-linux
      HB_USER_LDFLAGS=-target aarch64-linux
      HB_USER_DFLAGS=-target aarch64-linux

      Some target platforms need PIC mode even for static builds,
      in such case add:
      HB_USER_CFLAGS=-fPIC

    ; TOFIX: shared lib symlinks are not created when cross-building from
             Windows to Linux, could copy or adapt modern Windows counterpart

             olectl.h is missing when cross-building hbwin contrib from
             Linux to Windows (likely needs headers from Windows SDK)

             basically a non issue, but HB_CPU and HB_PLATFORM are not guessed
             from HB_ZIG_TARGET - conversion table seems to be doable, if
             someone is interested in making a patch.
2025-10-18 23:20:21 +02:00

105 lines
2.4 KiB
Makefile

ifeq ($(HB_BUILD_MODE),cpp)
HB_CMP := zig c++
else
HB_CMP := zig cc
endif
OBJ_EXT := .o
LIB_PREF := lib
LIB_EXT := .a
HB_DYN_COPT := -DHB_DYNLIB
CC := $(HB_CCPATH)$(HB_CCPREFIX)$(HB_CMP)$(HB_CCSUFFIX)
ifneq ($(filter --analyze, $(HB_USER_CFLAGS)),)
CC_IN :=
else
CC_IN := -c
endif
CC_OUT := -o
CFLAGS += -I. -I$(HB_HOST_INC)
CFLAGS += -Wno-error=date-time
# NOTE: no reproducible builds guaranteed by Harbour 3.2
ifneq ($(HB_ZIG_TARGET),)
LDFLAGS += -target $(HB_ZIG_TARGET)
DFLAGS += -target $(HB_ZIG_TARGET)
CFLAGS += -target $(HB_ZIG_TARGET)
endif
ifneq ($(HB_BUILD_WARN),no)
CFLAGS += -W -Wall
else
CFLAGS += -Wmissing-braces -Wreturn-type -Wformat
ifneq ($(HB_BUILD_MODE),cpp)
CFLAGS += -Wimplicit-int -Wimplicit-function-declaration
endif
endif
ifneq ($(HB_BUILD_OPTIM),no)
CFLAGS += -O3
endif
ifeq ($(HB_BUILD_DEBUG),yes)
CFLAGS += -g
endif
LD := $(CC)
LD_OUT := -o
LIBPATHS := $(foreach dir,$(LIB_DIR) $(SYSLIBPATHS),-L$(dir))
LDLIBS := $(foreach lib,$(HB_USER_LIBS) $(LIBS) $(SYSLIBS),-l$(lib))
LDFLAGS += $(LIBPATHS)
AR := zig ar
AR_RULE = ( $(AR) $(ARFLAGS) $(HB_AFLAGS) $(HB_USER_AFLAGS) rcs $(LIB_DIR)/$@ $(^F) $(ARSTRIP) ) || ( $(RM) $(subst /,$(DIRSEP),$(LIB_DIR)/$@) && $(FALSE) )
DY := $(CC)
DFLAGS += -shared $(LIBPATHS)
DY_OUT := -o$(subst x,x, )
DLIBS := $(foreach lib,$(HB_USER_LIBS) $(SYSLIBS),-l$(lib))
ifeq ($(HB_SHELL),nt)
define dynlib_object
@$(ECHO) $(ECHOQUOTE)$(subst \,/,$(file)) $(ECHOQUOTE) >> __dyn__.tmp
endef
# no-op under cmd.exe shell
define dynlib_ln
endef
# FIXME: symlinks for dynlibs generated when cross compiling from windows
# to unix are not created, decide if we want a copy or something else
#
# define dynlib_ln
# $(LN) $(subst /,\,$(DYN_DIR)\$(LIB_PREF)$(basename $@)$(LIB_EXT)) $(subst /,\,$(LIB_DIR)\$(LIB_PREF)$(basename $@)$(LIB_EXT))
# endef
else
define dynlib_object
@$(ECHO) -n $(ECHOQUOTE)$(subst \,/,$(file)) $(ECHOQUOTE) >> __dyn__.tmp
endef
define dynlib_ln
$(LN) $(@F) $(DYN_FILE_NVR) && $(LN) $(@F) $(DYN_FILE_CPT)
endef
endif
TMPSPEC := @__dyn__.tmp
define create_dynlib
$(if $(wildcard __dyn__.tmp),@$(RM) __dyn__.tmp,)
$(foreach file,$^,$(dynlib_object))
$(DY) $(DFLAGS) $(HB_USER_DFLAGS) $(DY_OUT)$(DYN_DIR)/$@ $(TMPSPEC) $(DLIBS) $(IMPLIBFLAGS) $(DYSTRIP) $(DYSTRIP)
$(dynlib_ln)
endef
DY_RULE = $(create_dynlib)
include $(TOP)$(ROOT)config/rules.mk