blob: ff452d8b01fa4cd29ad0eea080aa64a2fe546833 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001# To build your package using meson:
2#
3# include $(INCLUDE_DIR)/meson.mk
4# MESON_ARGS+=-Dfoo -Dbar=baz
5#
6# To pass additional environment variables to meson:
7#
8# MESON_VARS+=FOO=bar
9#
10# Default configure/compile/install targets are provided, but can be
11# overwritten if required:
12#
13# define Build/Configure
14# $(call Build/Configure/Meson)
15# ...
16# endef
17#
18# same for Build/Compile and Build/Install
19#
20# Host packages are built in the same fashion, just use these vars instead:
21#
22# MESON_HOST_ARGS+=-Dfoo -Dbar=baz
23# MESON_HOST_VARS+=FOO=bar
24
25MESON_DIR:=$(STAGING_DIR_HOST)/lib/meson
26
27MESON_HOST_BUILD_DIR:=$(HOST_BUILD_DIR)/openwrt-build
28MESON_HOST_VARS:=
29MESON_HOST_ARGS:=
30
31MESON_BUILD_DIR:=$(PKG_BUILD_DIR)/openwrt-build
32MESON_VARS:=
33MESON_ARGS:=
34
35ifneq ($(findstring i386,$(CONFIG_ARCH)),)
36MESON_ARCH:="x86"
37else ifneq ($(findstring powerpc64,$(CONFIG_ARCH)),)
38MESON_ARCH:="ppc64"
39else ifneq ($(findstring powerpc,$(CONFIG_ARCH)),)
40MESON_ARCH:="ppc"
41else ifneq ($(findstring mips64el,$(CONFIG_ARCH)),)
42MESON_ARCH:="mips64"
43else ifneq ($(findstring mipsel,$(CONFIG_ARCH)),)
44MESON_ARCH:="mips"
45else ifneq ($(findstring armeb,$(CONFIG_ARCH)),)
46MESON_ARCH:="arm"
47else
48MESON_ARCH:=$(CONFIG_ARCH)
49endif
50
51# this is undefined for just x64_64
52ifeq ($(origin CPU_TYPE),undefined)
53MESON_CPU:="generic"
54else
55MESON_CPU:="$(CPU_TYPE)$(if $(CPU_SUBTYPE),+$(CPU_SUBTYPE))"
56endif
57
58define Meson
59 $(2) $(STAGING_DIR_HOST)/bin/$(PYTHON) $(STAGING_DIR_HOST)/bin/meson.py $(1)
60endef
61
62define Meson/CreateNativeFile
63 $(STAGING_DIR_HOST)/bin/sed \
64 -e "s|@CC@|$(foreach BIN,$(HOSTCC),'$(BIN)',)|" \
65 -e "s|@CXX@|$(foreach BIN,$(HOSTCXX),'$(BIN)',)|" \
66 -e "s|@PKGCONFIG@|$(PKG_CONFIG)|" \
67 -e "s|@CMAKE@|$(STAGING_DIR_HOST)/bin/cmake|" \
68 -e "s|@PYTHON@|$(STAGING_DIR_HOST)/bin/python3|" \
69 -e "s|@CFLAGS@|$(foreach FLAG,$(HOST_CFLAGS) $(HOST_CPPFLAGS),'$(FLAG)',)|" \
70 -e "s|@CXXFLAGS@|$(foreach FLAG,$(HOST_CXXFLAGS) $(HOST_CPPFLAGS),'$(FLAG)',)|" \
71 -e "s|@LDFLAGS@|$(foreach FLAG,$(HOST_LDFLAGS),'$(FLAG)',)|" \
72 -e "s|@PREFIX@|$(HOST_BUILD_PREFIX)|" \
73 < $(MESON_DIR)/openwrt-native.txt.in \
74 > $(1)
75endef
76
77define Meson/CreateCrossFile
78 $(STAGING_DIR_HOST)/bin/sed \
79 -e "s|@CC@|$(foreach BIN,$(TARGET_CC),'$(BIN)',)|" \
80 -e "s|@CXX@|$(foreach BIN,$(TARGET_CXX),'$(BIN)',)|" \
81 -e "s|@LD@|$(foreach FLAG,$(TARGET_LINKER),'$(FLAG)',)|" \
82 -e "s|@AR@|$(TARGET_AR)|" \
83 -e "s|@STRIP@|$(TARGET_CROSS)strip|" \
84 -e "s|@NM@|$(TARGET_NM)|" \
85 -e "s|@PKGCONFIG@|$(PKG_CONFIG)|" \
86 -e "s|@CMAKE@|$(STAGING_DIR_HOST)/bin/cmake|" \
87 -e "s|@PYTHON@|$(STAGING_DIR_HOST)/bin/python3|" \
88 -e "s|@CFLAGS@|$(foreach FLAG,$(TARGET_CFLAGS) $(EXTRA_CFLAGS) $(TARGET_CPPFLAGS) $(EXTRA_CPPFLAGS),'$(FLAG)',)|" \
89 -e "s|@CXXFLAGS@|$(foreach FLAG,$(TARGET_CXXFLAGS) $(EXTRA_CXXFLAGS) $(TARGET_CPPFLAGS) $(EXTRA_CPPFLAGS),'$(FLAG)',)|" \
90 -e "s|@LDFLAGS@|$(foreach FLAG,$(TARGET_LDFLAGS) $(EXTRA_LDFLAGS),'$(FLAG)',)|" \
91 -e "s|@ARCH@|$(MESON_ARCH)|" \
92 -e "s|@CPU@|$(MESON_CPU)|" \
93 -e "s|@ENDIAN@|$(if $(CONFIG_BIG_ENDIAN),big,little)|" \
94 < $(MESON_DIR)/openwrt-cross.txt.in \
95 > $(1)
96endef
97
98define Host/Configure/Meson
99 $(call Meson/CreateNativeFile,$(HOST_BUILD_DIR)/openwrt-native.txt)
100 $(call Meson, \
101 setup \
102 --native-file $(HOST_BUILD_DIR)/openwrt-native.txt \
103 -Ddefault_library=static \
104 $(MESON_HOST_ARGS) \
105 $(MESON_HOST_BUILD_DIR) \
106 $(MESON_HOST_BUILD_DIR)/.., \
107 $(MESON_HOST_VARS))
108endef
109
110define Host/Compile/Meson
111 +$(NINJA) -C $(MESON_HOST_BUILD_DIR) $(1)
112endef
113
114define Host/Install/Meson
115 +$(NINJA) -C $(MESON_HOST_BUILD_DIR) install
116endef
117
118define Host/Uninstall/Meson
119 +$(NINJA) -C $(MESON_HOST_BUILD_DIR) uninstall || true
120endef
121
122define Build/Configure/Meson
123 $(call Meson/CreateNativeFile,$(PKG_BUILD_DIR)/openwrt-native.txt)
124 $(call Meson/CreateCrossFile,$(PKG_BUILD_DIR)/openwrt-cross.txt)
125 $(call Meson, \
126 setup \
127 --buildtype $(if $(CONFIG_DEBUG),debug,plain) \
128 --native-file $(PKG_BUILD_DIR)/openwrt-native.txt \
129 --cross-file $(PKG_BUILD_DIR)/openwrt-cross.txt \
130 -Ddefault_library=both \
131 $(MESON_ARGS) \
132 $(MESON_BUILD_DIR) \
133 $(MESON_BUILD_DIR)/.., \
134 $(MESON_VARS))
135endef
136
137define Build/Compile/Meson
138 +$(NINJA) -C $(MESON_BUILD_DIR) $(1)
139endef
140
141define Build/Install/Meson
142 +DESTDIR="$(PKG_INSTALL_DIR)" $(NINJA) -C $(MESON_BUILD_DIR) install
143endef
144
145Host/Configure=$(call Host/Configure/Meson)
146Host/Compile=$(call Host/Compile/Meson)
147Host/Install=$(call Host/Install/Meson)
148Host/Uninstall=$(call Host/Uninstall/Meson)
149Build/Configure=$(call Build/Configure/Meson)
150Build/Compile=$(call Build/Compile/Meson)
151Build/Install=$(call Build/Install/Meson)