images imagebuilder: Allow to add sanitized extra name
[openwrt.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         make image EXTRA_IMAGE_NAME="<string>" # Add this to the output image filename (sanitized)
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 LISTS_DIR:=$(subst $(space),/,$(patsubst %,..,$(subst /,$(space),$(TARGET_DIR))))$(DL_DIR)
57 OPKG:= \
58   IPKG_NO_SCRIPT=1 \
59   IPKG_TMP="$(TMP_DIR)/ipkgtmp" \
60   IPKG_INSTROOT="$(TARGET_DIR)" \
61   IPKG_CONF_DIR="$(TMP_DIR)" \
62   IPKG_OFFLINE_ROOT="$(TARGET_DIR)" \
63   $(STAGING_DIR_HOST)/bin/opkg \
64         -f $(TOPDIR)/repositories.conf \
65         --force-depends \
66         --force-overwrite \
67         --force-postinstall \
68         --cache $(DL_DIR) \
69         --lists-dir $(LISTS_DIR) \
70         --offline-root $(TARGET_DIR) \
71         --add-dest root:/ \
72         --add-arch all:100 \
73         --add-arch $(ARCH_PACKAGES):200
74
75 define Profile
76   $(eval $(call Profile/Default))
77   $(eval $(call Profile/$(1)))
78   ifeq ($(USER_PROFILE),)
79     USER_PROFILE:=$(1)
80   endif
81   $(1)_NAME:=$(NAME)
82   $(1)_PACKAGES:=$(PACKAGES)
83   PROFILE_NAMES += $(1)
84   PROFILE_LIST += \
85         echo '$(1):'; [ -z '$(NAME)' ] || echo '        $(NAME)'; echo '        Packages: $(PACKAGES)';
86 endef
87
88 include $(INCLUDE_DIR)/target.mk
89
90 staging_dir/host/.prereq-build: include/prereq-build.mk
91         mkdir -p tmp
92         rm -f tmp/.host.mk
93         @$(_SINGLE)$(NO_TRACE_MAKE) -j1 -r -s -f $(TOPDIR)/include/prereq-build.mk prereq 2>/dev/null || { \
94                 echo "Prerequisite check failed. Use FORCE=1 to override."; \
95                 false; \
96         }
97   ifneq ($(realpath $(TOPDIR)/include/prepare.mk),)
98         @$(_SINGLE)$(NO_TRACE_MAKE) -j1 -r -s -f $(TOPDIR)/include/prepare.mk prepare 2>/dev/null || { \
99                 echo "Preparation failed."; \
100                 false; \
101         }
102   endif
103         touch $@
104
105 _call_info: FORCE
106         echo 'Current Target: "$(BOARD)$(if $(SUBTARGET), ($(BOARDNAME)))"'
107         echo 'Default Packages: $(DEFAULT_PACKAGES)'
108         echo 'Available Profiles:'
109         echo; $(PROFILE_LIST)
110
111 BUILD_PACKAGES:=$(sort $(DEFAULT_PACKAGES) $(USER_PACKAGES) $($(USER_PROFILE)_PACKAGES) kernel)
112 # "-pkgname" in the package list means remove "pkgname" from the package list
113 BUILD_PACKAGES:=$(filter-out $(filter -%,$(BUILD_PACKAGES)) $(patsubst -%,%,$(filter -%,$(BUILD_PACKAGES))),$(BUILD_PACKAGES))
114 PACKAGES:=
115
116 _call_image: staging_dir/host/.prereq-build
117         echo 'Building images for $(BOARD)$(if $($(USER_PROFILE)_NAME), - $($(USER_PROFILE)_NAME))'
118         echo 'Packages: $(BUILD_PACKAGES)'
119         echo
120         rm -rf $(TARGET_DIR)
121         mkdir -p $(TARGET_DIR) $(BIN_DIR) $(TMP_DIR) $(DL_DIR)
122         if [ ! -f "$(PACKAGE_DIR)/Packages" ] || [ ! -f "$(PACKAGE_DIR)/Packages.gz" ] || [ "`find $(PACKAGE_DIR) -cnewer $(PACKAGE_DIR)/Packages.gz`" ]; then \
123                 echo "Package list missing or not up-to-date, generating it.";\
124                 $(MAKE) package_index; \
125         else \
126                 mkdir -p $(TARGET_DIR)/tmp; \
127                 $(OPKG) update || true; \
128         fi
129         $(MAKE) package_install
130 ifneq ($(USER_FILES),)
131         $(MAKE) copy_files
132 endif
133         $(MAKE) package_postinst
134         $(MAKE) build_image
135
136 package_index: FORCE
137         @echo
138         @echo Building package index...
139         @mkdir -p $(TMP_DIR) $(TARGET_DIR)/tmp
140         (cd $(PACKAGE_DIR); $(SCRIPT_DIR)/ipkg-make-index.sh . > Packages && \
141                 gzip -9nc Packages > Packages.gz \
142         ) >/dev/null 2>/dev/null
143         $(OPKG) update || true
144
145 package_install: FORCE
146         @echo
147         @echo Installing packages...
148         $(OPKG) install $(firstword $(wildcard $(PACKAGE_DIR)/libc_*.ipk $(PACKAGE_DIR)/base/libc_*.ipk))
149         $(OPKG) install $(firstword $(wildcard $(PACKAGE_DIR)/kernel_*.ipk $(PACKAGE_DIR)/base/kernel_*.ipk))
150         $(OPKG) install $(BUILD_PACKAGES)
151         rm -f $(TARGET_DIR)/usr/lib/opkg/lists/*
152
153 copy_files: FORCE
154         @echo
155         @echo Copying extra files
156         @$(call file_copy,$(USER_FILES)/*,$(TARGET_DIR)/)
157
158 package_postinst: FORCE
159         @echo
160         @echo Cleaning up
161         @rm -f $(TARGET_DIR)/tmp/opkg.lock
162         @echo
163         @echo Activating init scripts
164         @mkdir -p $(TARGET_DIR)/etc/rc.d
165         @( \
166                 cd $(TARGET_DIR); \
167                 for script in ./usr/lib/opkg/info/*.postinst; do \
168                         IPKG_INSTROOT=$(TARGET_DIR) $$(which bash) $$script; \
169                 done || true \
170         )
171         rm -f $(TARGET_DIR)/usr/lib/opkg/info/*.postinst
172         $(if $(CONFIG_CLEAN_IPKG),rm -rf $(TARGET_DIR)/usr/lib/opkg)
173
174 build_image: FORCE
175         @echo
176         @echo Building images...
177         $(NO_TRACE_MAKE) -C target/linux/$(BOARD)/image install TARGET_BUILD=1 IB=1 EXTRA_IMAGE_NAME="$(EXTRA_IMAGE_NAME)" \
178                 $(if $(USER_PROFILE),PROFILE="$(USER_PROFILE)")
179
180 clean:
181         rm -rf $(TMP_DIR) $(DL_DIR) $(TARGET_DIR) $(BIN_DIR)
182
183
184 info:
185         (unset PROFILE FILES PACKAGES MAKEFLAGS; $(MAKE) -s _call_info)
186
187 image:
188 ifneq ($(PROFILE),)
189   ifeq ($(filter $(PROFILE),$(PROFILE_NAMES)),)
190         @echo 'Profile "$(PROFILE)" does not exist!'
191         @echo 'Use "make info" to get a list of available profile names.'
192         @exit 1
193   endif
194 endif
195         (unset PROFILE FILES PACKAGES MAKEFLAGS; \
196         $(MAKE) _call_image \
197                 $(if $(PROFILE),USER_PROFILE="$(PROFILE)") \
198                 $(if $(FILES),USER_FILES="$(FILES)") \
199                 $(if $(PACKAGES),USER_PACKAGES="$(PACKAGES)") \
200                 $(if $(BIN_DIR),BIN_DIR="$(BIN_DIR)"))
201
202 .SILENT: help info image
203