4d0f1fe1159124c44266579ba5dc590a846e9216
[openwrt.git] / target / imagebuilder / files / Makefile
1 # Makefile for OpenWrt
2 #
3 # Copyright (C) 2007-2010 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_TMP="$(TOPDIR)/tmp/ipkgtmp" \
58   IPKG_INSTROOT="$(TARGET_DIR)" \
59   IPKG_CONF_DIR="$(TOPDIR)/tmp" \
60   IPKG_OFFLINE_ROOT="$(TARGET_DIR)" \
61   $(STAGING_DIR_HOST)/bin/opkg \
62         -f $(TOPDIR)/repositories.conf \
63         --force-depends \
64         --force-overwrite \
65         --force-postinstall \
66         --cache $(TOPDIR)/dl \
67         --offline-root $(TARGET_DIR) \
68         --add-dest root:/ \
69         --add-arch all:100 \
70         --add-arch $(ARCH_PACKAGES):200
71
72 define Profile
73   $(eval $(call Profile/Default))
74   $(eval $(call Profile/$(1)))
75   ifeq ($(USER_PROFILE),)
76     USER_PROFILE:=$(1)
77   endif
78   $(1)_NAME:=$(NAME)
79   $(1)_PACKAGES:=$(PACKAGES)
80   PROFILE_LIST += \
81         echo '$(1):'; [ -z '$(NAME)' ] || echo '        $(NAME)'; echo '        Packages: $(PACKAGES)';
82 endef
83
84 include $(INCLUDE_DIR)/target.mk
85
86 _call_info: FORCE
87         echo 'Current Target: "$(BOARD)$(if $(SUBTARGET), ($(BOARDNAME)))"'
88         echo 'Default Packages: $(DEFAULT_PACKAGES)'
89         echo 'Available Profiles:'
90         echo; $(PROFILE_LIST)
91
92 BUILD_PACKAGES:=$(sort $(DEFAULT_PACKAGES) $(USER_PACKAGES) $($(USER_PROFILE)_PACKAGES) kernel)
93 # "-pkgname" in the package list means remove "pkgname" from the package list
94 BUILD_PACKAGES:=$(filter-out $(filter -%,$(BUILD_PACKAGES)) $(patsubst -%,%,$(filter -%,$(BUILD_PACKAGES))),$(BUILD_PACKAGES))
95 PACKAGES:=
96
97 _call_image:
98         echo 'Building images for $(BOARD)$(if $($(USER_PROFILE)_NAME), - $($(USER_PROFILE)_NAME))'
99         echo 'Packages: $(BUILD_PACKAGES)'
100         echo
101         rm -rf $(TARGET_DIR)
102         mkdir -p $(TARGET_DIR) $(BIN_DIR) $(TMP_DIR)
103         if [ ! -f "$(PACKAGE_DIR)/Packages" ] || [ ! -f "$(PACKAGE_DIR)/Packages.gz" ] || [ "`find $(PACKAGE_DIR) -cnewer $(PACKAGE_DIR)/Packages.gz`" ]; then \
104                 echo "Package list missing or not up-to-date, generating it.";\
105                 $(MAKE) package_index; \
106         else \
107                 mkdir -p $(TARGET_DIR)/tmp; \
108                 $(OPKG) update; \
109         fi
110         $(MAKE) package_install
111 ifneq ($(USER_FILES),)
112         $(MAKE) copy_files
113 endif
114         $(MAKE) package_postinst
115         $(MAKE) build_image
116
117 package_index: FORCE
118         @echo
119         @echo Building package index...
120         @mkdir -p $(TOPDIR)/tmp $(TOPDIR)/dl $(TARGET_DIR)/tmp
121         (cd $(PACKAGE_DIR); $(SCRIPT_DIR)/ipkg-make-index.sh . > Packages && \
122                 gzip -9c Packages > Packages.gz \
123         ) >/dev/null 2>/dev/null
124         $(OPKG) update
125
126 package_install: FORCE
127         @echo
128         @echo Installing packages...
129         $(OPKG) install $(PACKAGE_DIR)/libc_*.ipk
130         $(OPKG) install $(PACKAGE_DIR)/kernel_*.ipk
131         $(OPKG) install $(BUILD_PACKAGES)
132         rm -f $(TARGET_DIR)/usr/lib/opkg/lists/*
133
134 copy_files: FORCE
135         @echo
136         @echo Copying extra files
137         @( cd $(USER_FILES); find -type f ) | \
138                 ( cd $(TARGET_DIR); while :; do \
139                         read FILE; \
140                         [ -z "$$FILE" ] && break; \
141                         [ -L "$$FILE" ] || continue; \
142                         echo "Removing symlink $(TARGET_DIR)/$$FILE"; \
143                         rm -f "$$FILE"; \
144                 done; )
145         $(CP) $(USER_FILES)/* $(TARGET_DIR)/
146
147 package_postinst: FORCE
148         @echo
149         @echo Cleaning up
150         @rm -f $(TARGET_DIR)/tmp/opkg.lock
151         @echo
152         @echo Activating init scripts
153         @( \
154                 cd $(TARGET_DIR); \
155                 for script in ./etc/init.d/*; do \
156                         grep '#!/bin/sh /etc/rc.common' $$script >/dev/null || continue; \
157                         IPKG_INSTROOT=$(TARGET_DIR) $(which bash) ./etc/rc.common $$script enable; \
158                 done || true; \
159         )
160         $(if $(CONFIG_CLEAN_IPKG),rm -rf $(TARGET_DIR)/usr/lib/opkg)
161
162 build_image: FORCE
163         @echo
164         @echo Building images...
165         $(NO_TRACE_MAKE) -C target/linux/$(BOARD)/image install TARGET_BUILD=1 IB=1 \
166                 $(if $(USER_PROFILE),PROFILE="$(USER_PROFILE)")
167
168 clean:
169         rm -rf $(TOPDIR)/tmp $(TOPDIR)/dl $(TARGET_DIR) $(BIN_DIR)
170
171
172 info:
173         (unset PROFILE FILES PACKAGES MAKEFLAGS; $(MAKE) -s _call_info)
174
175 image:
176         (unset PROFILE FILES PACKAGES MAKEFLAGS; \
177         $(MAKE) _call_image \
178                 $(if $(PROFILE),USER_PROFILE="$(PROFILE)") \
179                 $(if $(FILES),USER_FILES="$(FILES)") \
180                 $(if $(PACKAGES),USER_PACKAGES="$(PACKAGES)") \
181                 $(if $(BIN_DIR),BIN_DIR="$(BIN_DIR)"))
182
183 .SILENT: help info image
184