imagebuilder: run build prereq checks before building image to set up host commands...
[librecmc/librecmc.git] / target / imagebuilder / files / Makefile
1 # Makefile for OpenWrt
2 #
3 # Copyright (C) 2007-2015 OpenWrt.org
4 #
5 # This is free software, licensed under the GNU General Public License v2.
6 # See /LICENSE for more information.
7 #
8
9 TOPDIR:=${CURDIR}
10 LC_ALL:=C
11 LANG:=C
12 export TOPDIR LC_ALL LANG
13 export OPENWRT_VERBOSE=s
14 all: help
15
16 include $(TOPDIR)/include/host.mk
17
18 ifneq ($(OPENWRT_BUILD),1)
19   override OPENWRT_BUILD=1
20   export OPENWRT_BUILD
21 endif
22
23 include rules.mk
24 include $(INCLUDE_DIR)/debug.mk
25 include $(INCLUDE_DIR)/depends.mk
26
27 include $(INCLUDE_DIR)/version.mk
28 export REVISION
29
30 define Helptext
31 Available Commands:
32         help:   This help text
33         info:   Show a list of available target profiles
34         clean:  Remove images and temporary build files
35         image:  Build an image (see below for more information).
36
37 Building images:
38         By default 'make image' will create an image with the default
39         target profile and package set. You can use the following parameters
40         to change that:
41
42         make image PROFILE="<profilename>" # override the default target profile
43         make image PACKAGES="<pkg1> [<pkg2> [<pkg3> ...]]" # include extra packages
44         make image FILES="<path>" # include extra files from <path>
45         make image BIN_DIR="<path>" # alternative output directory for the images
46
47 endef
48 $(eval $(call shexport,Helptext))
49
50 help: FORCE
51         echo "$$$(call shvar,Helptext)"
52
53
54 # override variables from rules.mk
55 PACKAGE_DIR:=$(TOPDIR)/packages
56 OPKG:= \
57   IPKG_NO_SCRIPT=1 \
58   IPKG_TMP="$(TMP_DIR)/ipkgtmp" \
59   IPKG_INSTROOT="$(TARGET_DIR)" \
60   IPKG_CONF_DIR="$(TMP_DIR)" \
61   IPKG_OFFLINE_ROOT="$(TARGET_DIR)" \
62   $(STAGING_DIR_HOST)/bin/opkg \
63         -f $(TOPDIR)/repositories.conf \
64         --force-depends \
65         --force-overwrite \
66         --force-postinstall \
67         --cache $(DL_DIR) \
68         --offline-root $(TARGET_DIR) \
69         --add-dest root:/ \
70         --add-arch all:100 \
71         --add-arch $(ARCH_PACKAGES):200
72
73 define Profile
74   $(eval $(call Profile/Default))
75   $(eval $(call Profile/$(1)))
76   ifeq ($(USER_PROFILE),)
77     USER_PROFILE:=$(1)
78   endif
79   $(1)_NAME:=$(NAME)
80   $(1)_PACKAGES:=$(PACKAGES)
81   PROFILE_NAMES += $(1)
82   PROFILE_LIST += \
83         echo '$(1):'; [ -z '$(NAME)' ] || echo '        $(NAME)'; echo '        Packages: $(PACKAGES)';
84 endef
85
86 include $(INCLUDE_DIR)/target.mk
87
88 staging_dir/host/.prereq-build: include/prereq-build.mk
89         mkdir -p tmp
90         rm -f tmp/.host.mk
91         @$(_SINGLE)$(NO_TRACE_MAKE) -j1 -r -s -f $(TOPDIR)/include/prereq-build.mk prereq 2>/dev/null || { \
92                 echo "Prerequisite check failed. Use FORCE=1 to override."; \
93                 false; \
94         }
95   ifneq ($(realpath $(TOPDIR)/include/prepare.mk),)
96         @$(_SINGLE)$(NO_TRACE_MAKE) -j1 -r -s -f $(TOPDIR)/include/prepare.mk prepare 2>/dev/null || { \
97                 echo "Preparation failed."; \
98                 false; \
99         }
100   endif
101         touch $@
102
103 _call_info: FORCE
104         echo 'Current Target: "$(BOARD)$(if $(SUBTARGET), ($(BOARDNAME)))"'
105         echo 'Default Packages: $(DEFAULT_PACKAGES)'
106         echo 'Available Profiles:'
107         echo; $(PROFILE_LIST)
108
109 BUILD_PACKAGES:=$(sort $(DEFAULT_PACKAGES) $(USER_PACKAGES) $($(USER_PROFILE)_PACKAGES) kernel)
110 # "-pkgname" in the package list means remove "pkgname" from the package list
111 BUILD_PACKAGES:=$(filter-out $(filter -%,$(BUILD_PACKAGES)) $(patsubst -%,%,$(filter -%,$(BUILD_PACKAGES))),$(BUILD_PACKAGES))
112 PACKAGES:=
113
114 _call_image: staging_dir/host/.prereq-build
115         echo 'Building images for $(BOARD)$(if $($(USER_PROFILE)_NAME), - $($(USER_PROFILE)_NAME))'
116         echo 'Packages: $(BUILD_PACKAGES)'
117         echo
118         rm -rf $(TARGET_DIR)
119         mkdir -p $(TARGET_DIR) $(BIN_DIR) $(TMP_DIR) $(DL_DIR)
120         if [ ! -f "$(PACKAGE_DIR)/Packages" ] || [ ! -f "$(PACKAGE_DIR)/Packages.gz" ] || [ "`find $(PACKAGE_DIR) -cnewer $(PACKAGE_DIR)/Packages.gz`" ]; then \
121                 echo "Package list missing or not up-to-date, generating it.";\
122                 $(MAKE) package_index; \
123         else \
124                 mkdir -p $(TARGET_DIR)/tmp; \
125                 $(OPKG) update || true; \
126         fi
127         $(MAKE) package_install
128 ifneq ($(USER_FILES),)
129         $(MAKE) copy_files
130 endif
131         $(MAKE) package_postinst
132         $(MAKE) build_image
133
134 package_index: FORCE
135         @echo
136         @echo Building package index...
137         @mkdir -p $(TMP_DIR) $(TARGET_DIR)/tmp
138         (cd $(PACKAGE_DIR); $(SCRIPT_DIR)/ipkg-make-index.sh . > Packages && \
139                 gzip -9nc Packages > Packages.gz \
140         ) >/dev/null 2>/dev/null
141         $(OPKG) update || true
142
143 package_install: FORCE
144         @echo
145         @echo Installing packages...
146         $(OPKG) install $(firstword $(wildcard $(PACKAGE_DIR)/libc_*.ipk $(PACKAGE_DIR)/base/libc_*.ipk))
147         $(OPKG) install $(firstword $(wildcard $(PACKAGE_DIR)/kernel_*.ipk $(PACKAGE_DIR)/base/kernel_*.ipk))
148         $(OPKG) install $(BUILD_PACKAGES)
149         rm -f $(TARGET_DIR)/usr/lib/opkg/lists/*
150
151 copy_files: FORCE
152         @echo
153         @echo Copying extra files
154         @$(call file_copy,$(USER_FILES)/*,$(TARGET_DIR)/)
155
156 package_postinst: FORCE
157         @echo
158         @echo Cleaning up
159         @rm -f $(TARGET_DIR)/tmp/opkg.lock
160         @echo
161         @echo Activating init scripts
162         @mkdir -p $(TARGET_DIR)/etc/rc.d
163         @( \
164                 cd $(TARGET_DIR); \
165                 for script in ./usr/lib/opkg/info/*.postinst; do \
166                         IPKG_INSTROOT=$(TARGET_DIR) $$(which bash) $$script; \
167                 done || true \
168         )
169         rm -f $(TARGET_DIR)/usr/lib/opkg/info/*.postinst
170         $(if $(CONFIG_CLEAN_IPKG),rm -rf $(TARGET_DIR)/usr/lib/opkg)
171
172 build_image: FORCE
173         @echo
174         @echo Building images...
175         $(NO_TRACE_MAKE) -C target/linux/$(BOARD)/image install TARGET_BUILD=1 IB=1 \
176                 $(if $(USER_PROFILE),PROFILE="$(USER_PROFILE)")
177
178 clean:
179         rm -rf $(TMP_DIR) $(DL_DIR) $(TARGET_DIR) $(BIN_DIR)
180
181
182 info:
183         (unset PROFILE FILES PACKAGES MAKEFLAGS; $(MAKE) -s _call_info)
184
185 image:
186 ifneq ($(PROFILE),)
187   ifeq ($(filter $(PROFILE),$(PROFILE_NAMES)),)
188         @echo 'Profile "$(PROFILE)" does not exist!'
189         @echo 'Use "make info" to get a list of available profile names.'
190         @exit 1
191   endif
192 endif
193         (unset PROFILE FILES PACKAGES MAKEFLAGS; \
194         $(MAKE) _call_image \
195                 $(if $(PROFILE),USER_PROFILE="$(PROFILE)") \
196                 $(if $(FILES),USER_FILES="$(FILES)") \
197                 $(if $(PACKAGES),USER_PACKAGES="$(PACKAGES)") \
198                 $(if $(BIN_DIR),BIN_DIR="$(BIN_DIR)"))
199
200 .SILENT: help info image
201