1# SPDX-License-Identifier: GPL-2.0-or-later 2# 3# Device Tree Compiler 4# 5 6# 7# Version information will be constructed in this order: 8# EXTRAVERSION might be "-rc", for example. 9# LOCAL_VERSION is likely from command line. 10# CONFIG_LOCALVERSION from some future config system. 11# 12VERSION = 1 13PATCHLEVEL = 6 14SUBLEVEL = 0 15EXTRAVERSION = 16LOCAL_VERSION = 17CONFIG_LOCALVERSION = 18 19# Control the assumptions made (e.g. risking security issues) in the code. 20# See libfdt_internal.h for details 21ASSUME_MASK ?= 0 22 23CPPFLAGS = -I libfdt -I . -DFDT_ASSUME_MASK=$(ASSUME_MASK) 24WARNINGS = -Wall -Wpointer-arith -Wcast-qual -Wnested-externs \ 25 -Wstrict-prototypes -Wmissing-prototypes -Wredundant-decls -Wshadow 26CFLAGS = -g -Os $(SHAREDLIB_CFLAGS) -Werror $(WARNINGS) $(EXTRA_CFLAGS) 27 28BISON = bison 29LEX = flex 30SWIG = swig 31PKG_CONFIG ?= pkg-config 32PYTHON ?= python3 33 34INSTALL = /usr/bin/install 35INSTALL_PROGRAM = $(INSTALL) 36INSTALL_LIB = $(INSTALL) 37INSTALL_DATA = $(INSTALL) -m 644 38INSTALL_SCRIPT = $(INSTALL) 39DESTDIR = 40PREFIX = $(HOME) 41BINDIR = $(PREFIX)/bin 42LIBDIR = $(PREFIX)/lib 43INCLUDEDIR = $(PREFIX)/include 44 45HOSTOS := $(shell uname -s | tr '[:upper:]' '[:lower:]' | \ 46 sed -e 's/\(cygwin\|msys\).*/\1/') 47 48NO_PYTHON ?= 0 49 50NO_VALGRIND := $(shell $(PKG_CONFIG) --exists valgrind; echo $$?) 51ifeq ($(NO_VALGRIND),1) 52 CPPFLAGS += -DNO_VALGRIND 53else 54 CFLAGS += $(shell $(PKG_CONFIG) --cflags valgrind) 55endif 56 57NO_YAML := $(shell $(PKG_CONFIG) --exists yaml-0.1; echo $$?) 58ifeq ($(NO_YAML),1) 59 CFLAGS += -DNO_YAML 60else 61 LDLIBS_dtc += $(shell $(PKG_CONFIG) --libs yaml-0.1) 62endif 63 64ifeq ($(HOSTOS),darwin) 65SHAREDLIB_EXT = dylib 66SHAREDLIB_CFLAGS = -fPIC 67SHAREDLIB_LDFLAGS = -fPIC -dynamiclib -Wl,-install_name -Wl, 68else ifeq ($(HOSTOS),$(filter $(HOSTOS),msys cygwin)) 69SHAREDLIB_EXT = so 70SHAREDLIB_CFLAGS = 71SHAREDLIB_LDFLAGS = -shared -Wl,--version-script=$(LIBFDT_version) -Wl,-soname, 72else 73SHAREDLIB_EXT = so 74SHAREDLIB_CFLAGS = -fPIC 75SHAREDLIB_LDFLAGS = -fPIC -shared -Wl,--version-script=$(LIBFDT_version) -Wl,-soname, 76endif 77 78# 79# Overall rules 80# 81ifdef V 82VECHO = : 83else 84VECHO = echo " " 85ARFLAGS = rc 86.SILENT: 87endif 88 89NODEPTARGETS = clean 90ifeq ($(MAKECMDGOALS),) 91DEPTARGETS = all 92else 93DEPTARGETS = $(filter-out $(NODEPTARGETS),$(MAKECMDGOALS)) 94endif 95 96# 97# Rules for versioning 98# 99 100DTC_VERSION = $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION) 101VERSION_FILE = version_gen.h 102 103CONFIG_SHELL := $(shell if [ -x "$$BASH" ]; then echo $$BASH; \ 104 else if [ -x /bin/bash ]; then echo /bin/bash; \ 105 else echo sh; fi ; fi) 106 107nullstring := 108space := $(nullstring) # end of line 109 110localver_config = $(subst $(space),, $(string) \ 111 $(patsubst "%",%,$(CONFIG_LOCALVERSION))) 112 113localver_cmd = $(subst $(space),, $(string) \ 114 $(patsubst "%",%,$(LOCALVERSION))) 115 116localver_scm = $(shell $(CONFIG_SHELL) ./scripts/setlocalversion) 117localver_full = $(localver_config)$(localver_cmd)$(localver_scm) 118 119dtc_version = $(DTC_VERSION)$(localver_full) 120 121# Contents of the generated version file. 122define filechk_version 123 (echo "#define DTC_VERSION \"DTC $(dtc_version)\""; ) 124endef 125 126define filechk 127 set -e; \ 128 echo ' CHK $@'; \ 129 mkdir -p $(dir $@); \ 130 $(filechk_$(1)) < $< > $@.tmp; \ 131 if [ -r $@ ] && cmp -s $@ $@.tmp; then \ 132 rm -f $@.tmp; \ 133 else \ 134 echo ' UPD $@'; \ 135 mv -f $@.tmp $@; \ 136 fi; 137endef 138 139 140include Makefile.convert-dtsv0 141include Makefile.dtc 142include Makefile.utils 143 144BIN += convert-dtsv0 145BIN += dtc 146BIN += fdtdump 147BIN += fdtget 148BIN += fdtput 149BIN += fdtoverlay 150 151SCRIPTS = dtdiff 152 153all: $(BIN) libfdt 154 155# We need both Python and swig to build/install pylibfdt. 156# This builds the given make ${target} if those deps are found. 157check_python_deps = \ 158 if $(PKG_CONFIG) --cflags $(PYTHON) >/dev/null 2>&1; then \ 159 if which swig >/dev/null 2>&1; then \ 160 can_build=yes; \ 161 fi; \ 162 fi; \ 163 if [ "$${can_build}" = "yes" ]; then \ 164 $(MAKE) $${target}; \ 165 else \ 166 echo "\#\# Skipping pylibfdt (install python dev and swig to build)"; \ 167 fi ; 168 169.PHONY: maybe_pylibfdt 170maybe_pylibfdt: FORCE 171 target=pylibfdt; $(check_python_deps) 172 173ifeq ($(NO_PYTHON),0) 174all: maybe_pylibfdt 175endif 176 177 178ifneq ($(DEPTARGETS),) 179ifneq ($(MAKECMDGOALS),libfdt) 180-include $(DTC_OBJS:%.o=%.d) 181-include $(CONVERT_OBJS:%.o=%.d) 182-include $(FDTDUMP_OBJS:%.o=%.d) 183-include $(FDTGET_OBJS:%.o=%.d) 184-include $(FDTPUT_OBJS:%.o=%.d) 185-include $(FDTOVERLAY_OBJS:%.o=%.d) 186endif 187endif 188 189 190 191# 192# Rules for libfdt 193# 194LIBFDT_dir = libfdt 195LIBFDT_archive = $(LIBFDT_dir)/libfdt.a 196LIBFDT_lib = $(LIBFDT_dir)/$(LIBFDT_LIB) 197LIBFDT_include = $(addprefix $(LIBFDT_dir)/,$(LIBFDT_INCLUDES)) 198LIBFDT_version = $(addprefix $(LIBFDT_dir)/,$(LIBFDT_VERSION)) 199 200include $(LIBFDT_dir)/Makefile.libfdt 201 202.PHONY: libfdt 203libfdt: $(LIBFDT_archive) $(LIBFDT_lib) 204 205$(LIBFDT_archive): $(addprefix $(LIBFDT_dir)/,$(LIBFDT_OBJS)) 206 207$(LIBFDT_lib): $(addprefix $(LIBFDT_dir)/,$(LIBFDT_OBJS)) $(LIBFDT_version) 208 @$(VECHO) LD $@ 209 $(CC) $(LDFLAGS) $(SHAREDLIB_LDFLAGS)$(LIBFDT_soname) -o $(LIBFDT_lib) \ 210 $(addprefix $(LIBFDT_dir)/,$(LIBFDT_OBJS)) 211 ln -sf $(LIBFDT_LIB) $(LIBFDT_dir)/$(LIBFDT_soname) 212 213ifneq ($(DEPTARGETS),) 214-include $(LIBFDT_OBJS:%.o=$(LIBFDT_dir)/%.d) 215endif 216 217# This stops make from generating the lex and bison output during 218# auto-dependency computation, but throwing them away as an 219# intermediate target and building them again "for real" 220.SECONDARY: $(DTC_GEN_SRCS) $(CONVERT_GEN_SRCS) 221 222install-bin: all $(SCRIPTS) 223 @$(VECHO) INSTALL-BIN 224 $(INSTALL) -d $(DESTDIR)$(BINDIR) 225 $(INSTALL_PROGRAM) $(BIN) $(DESTDIR)$(BINDIR) 226 $(INSTALL_SCRIPT) $(SCRIPTS) $(DESTDIR)$(BINDIR) 227 228install-lib: all 229 @$(VECHO) INSTALL-LIB 230 $(INSTALL) -d $(DESTDIR)$(LIBDIR) 231 $(INSTALL_LIB) $(LIBFDT_lib) $(DESTDIR)$(LIBDIR) 232 ln -sf $(notdir $(LIBFDT_lib)) $(DESTDIR)$(LIBDIR)/$(LIBFDT_soname) 233 ln -sf $(LIBFDT_soname) $(DESTDIR)$(LIBDIR)/libfdt.$(SHAREDLIB_EXT) 234 $(INSTALL_DATA) $(LIBFDT_archive) $(DESTDIR)$(LIBDIR) 235 236install-includes: 237 @$(VECHO) INSTALL-INC 238 $(INSTALL) -d $(DESTDIR)$(INCLUDEDIR) 239 $(INSTALL_DATA) $(LIBFDT_include) $(DESTDIR)$(INCLUDEDIR) 240 241install: install-bin install-lib install-includes 242 243.PHONY: maybe_install_pylibfdt 244maybe_install_pylibfdt: FORCE 245 target=install_pylibfdt; $(check_python_deps) 246 247ifeq ($(NO_PYTHON),0) 248install: maybe_install_pylibfdt 249endif 250 251$(VERSION_FILE): Makefile FORCE 252 $(call filechk,version) 253 254 255dtc: $(DTC_OBJS) 256 257convert-dtsv0: $(CONVERT_OBJS) 258 @$(VECHO) LD $@ 259 $(LINK.c) -o $@ $^ 260 261fdtdump: $(FDTDUMP_OBJS) 262 263fdtget: $(FDTGET_OBJS) $(LIBFDT_lib) 264 265fdtput: $(FDTPUT_OBJS) $(LIBFDT_lib) 266 267fdtoverlay: $(FDTOVERLAY_OBJS) $(LIBFDT_lib) 268 269dist: 270 git archive --format=tar --prefix=dtc-$(dtc_version)/ HEAD \ 271 > ../dtc-$(dtc_version).tar 272 cat ../dtc-$(dtc_version).tar | \ 273 gzip -9 > ../dtc-$(dtc_version).tar.gz 274 275 276# 277# Rules for pylibfdt 278# 279PYLIBFDT_dir = pylibfdt 280 281include $(PYLIBFDT_dir)/Makefile.pylibfdt 282 283.PHONY: pylibfdt 284pylibfdt: $(PYLIBFDT_dir)/_libfdt.so 285 286# 287# Release signing and uploading 288# This is for maintainer convenience, don't try this at home. 289# 290ifeq ($(MAINTAINER),y) 291GPG = gpg2 292KUP = kup 293KUPDIR = /pub/software/utils/dtc 294 295kup: dist 296 $(GPG) --detach-sign --armor -o ../dtc-$(dtc_version).tar.sign \ 297 ../dtc-$(dtc_version).tar 298 $(KUP) put ../dtc-$(dtc_version).tar.gz ../dtc-$(dtc_version).tar.sign \ 299 $(KUPDIR)/dtc-$(dtc_version).tar.gz 300endif 301 302tags: FORCE 303 rm -f tags 304 find . \( -name tests -type d -prune \) -o \ 305 \( ! -name '*.tab.[ch]' ! -name '*.lex.c' \ 306 -name '*.[chly]' -type f -print \) | xargs ctags -a 307 308# 309# Testsuite rules 310# 311TESTS_PREFIX=tests/ 312 313TESTS_BIN += dtc 314TESTS_BIN += convert-dtsv0 315TESTS_BIN += fdtput 316TESTS_BIN += fdtget 317TESTS_BIN += fdtdump 318TESTS_BIN += fdtoverlay 319ifeq ($(NO_PYTHON),0) 320TESTS_PYLIBFDT += maybe_pylibfdt 321endif 322 323ifneq ($(MAKECMDGOALS),libfdt) 324include tests/Makefile.tests 325endif 326 327# 328# Clean rules 329# 330STD_CLEANFILES = *~ *.o *.$(SHAREDLIB_EXT) *.d *.a *.i *.s core a.out vgcore.* \ 331 *.tab.[ch] *.lex.c *.output 332 333clean: libfdt_clean pylibfdt_clean tests_clean 334 @$(VECHO) CLEAN 335 rm -f $(STD_CLEANFILES) 336 rm -f $(VERSION_FILE) 337 rm -f $(BIN) 338 rm -f dtc-*.tar dtc-*.tar.sign dtc-*.tar.asc 339 340# 341# Generic compile rules 342# 343%: %.o 344 @$(VECHO) LD $@ 345 $(LINK.c) -o $@ $^ $(LDLIBS_$*) 346 347%.o: %.c 348 @$(VECHO) CC $@ 349 $(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $< 350 351%.o: %.S 352 @$(VECHO) AS $@ 353 $(CC) $(CPPFLAGS) $(AFLAGS) -D__ASSEMBLY__ -o $@ -c $< 354 355%.d: %.c 356 @$(VECHO) DEP $< 357 $(CC) $(CPPFLAGS) $(CFLAGS) -MM -MG -MT "$*.o $@" $< > $@ 358 359%.d: %.S 360 @$(VECHO) DEP $< 361 $(CC) $(CPPFLAGS) -MM -MG -MT "$*.o $@" $< > $@ 362 363%.i: %.c 364 @$(VECHO) CPP $@ 365 $(CC) $(CPPFLAGS) -E $< > $@ 366 367%.s: %.c 368 @$(VECHO) CC -S $@ 369 $(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -S $< 370 371%.a: 372 @$(VECHO) AR $@ 373 $(AR) $(ARFLAGS) $@ $^ 374 375%.lex.c: %.l 376 @$(VECHO) LEX $@ 377 $(LEX) -o$@ $< 378 379%.tab.c %.tab.h %.output: %.y 380 @$(VECHO) BISON $@ 381 $(BISON) -b $(basename $(basename $@)) -d $< 382 383FORCE: 384