1#
2#    Make pre-include environment Makefile.
3#
4#    Copyright (C) 2009, Cisco Systems Inc.
5#
6#    This program is free software; you can redistribute it and/or modify
7#    it under the terms of the GNU General Public License as published by
8#    the Free Software Foundation; either version 2 of the License, or
9#    (at your option) any later version.
10#
11#    This program is distributed in the hope that it will be useful,
12#    but WITHOUT ANY WARRANTY; without even the implied warranty of
13#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14#    GNU General Public License for more details.
15#
16#    You should have received a copy of the GNU General Public License along
17#    with this program; if not, write to the Free Software Foundation, Inc.,
18#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19#
20# Ngie Cooper, September 2009
21#
22# This Makefile must be included first. NO IF'S, AND'S, OR BUT'S.
23#
24# This sets the stage for all operations required within Makefiles.
25#
26
27ifndef ENV_PRE_LOADED
28ENV_PRE_LOADED = 1
29
30# "out-of-build-tree" build.
31BUILD_TREE_BUILDDIR_INSTALL	:= 1
32# "in-srcdir" build / install.
33BUILD_TREE_SRCDIR_INSTALL	:= 2
34# "in-srcdir" build, non-srcdir install.
35BUILD_TREE_NONSRCDIR_INSTALL	:= 3
36# configure not run.
37BUILD_TREE_UNCONFIGURED		:= 4
38
39ifndef MAKE_VERSION_CHECK
40export MAKE_VERSION_CHECK = 1
41ifneq ($(firstword $(sort 3.80 $(MAKE_VERSION))),3.80)
42$(error Your version of make $(MAKE_VERSION) is too old. Upgrade to at least 3.80; 3.81+ is preferred)
43else
44ifneq ($(filter 3.80%,$(MAKE_VERSION)),)
45export MAKE_3_80_COMPAT	:= 1
46endif # make 3.80?
47endif # At least make 3.80?
48endif # MAKE_VERSION_CHECK
49
50# Get the absolute path for the source directory.
51top_srcdir			?= $(error You must define top_srcdir before including this file)
52
53include $(top_srcdir)/include/mk/functions.mk
54
55# Where's the root source directory?
56ifdef MAKE_3_80_COMPAT
57abs_top_srcdir			:= $(call MAKE_3_80_abspath,$(top_srcdir))
58else
59abs_top_srcdir			:= $(abspath $(top_srcdir))
60endif
61
62#
63# Where's the root object directory?
64#
65# Just in case it's not specified, set it to the top srcdir (because the user
66# must not have wanted out of build tree support)...
67#
68top_builddir			?= $(top_srcdir)
69
70# We need the absolute path...
71ifdef MAKE_3_80_COMPAT
72abs_top_builddir		:= $(call MAKE_3_80_abspath,$(top_builddir))
73else
74abs_top_builddir		:= $(abspath $(top_builddir))
75endif
76
77# Where's the root object directory?
78builddir			:= .
79
80abs_builddir			:= $(CURDIR)
81
82cwd_rel_from_top		:= $(subst $(abs_top_builddir),,$(abs_builddir))
83
84# Where's the source located at? Squish all of the / away by using abspath...
85ifdef MAKE_3_80_COMPAT
86abs_srcdir			:= $(call MAKE_3_80_abspath,$(abs_top_srcdir)/$(cwd_rel_from_top))
87else
88abs_srcdir			:= $(abspath $(abs_top_srcdir)/$(cwd_rel_from_top))
89endif
90
91srcdir				:= $(strip $(subst $(abs_top_srcdir)/,,$(abs_srcdir)))
92
93ifeq ($(srcdir),)
94srcdir				:= .
95endif
96
97# If config.mk or features.mk doesn't exist it's not an error for some targets
98# which are filtered below (e.g. clean). However these config files may be
99# needed for those targets (eg. the open posix testsuite is not cleaned even if
100# it's enabled by configure) thus it would be wise to do silent inclusion.
101ifneq ("$(wildcard $(abs_top_builddir)/include/mk/config.mk)","")
102include $(abs_top_builddir)/include/mk/config.mk
103endif
104ifneq ("$(wildcard $(abs_top_builddir)/include/mk/features.mk)","")
105include $(abs_top_builddir)/include/mk/features.mk
106endif
107
108# autotools, *clean, and help don't require config.mk, features.mk, etc...
109ifeq ($(filter autotools %clean .gitignore gitignore.% help,$(MAKECMDGOALS)),)
110
111include $(abs_top_builddir)/include/mk/config.mk
112include $(abs_top_builddir)/include/mk/features.mk
113
114# START out-of-build-tree check.
115ifneq ($(abs_builddir),$(abs_srcdir))
116BUILD_TREE_STATE		:= $(BUILD_TREE_BUILDDIR_INSTALL)
117else
118# Else, not out of build tree..
119
120# START srcdir build-tree install checks
121ifeq ($(strip $(DESTDIR)$(prefix)),)
122BUILD_TREE_STATE		:= $(BUILD_TREE_SRCDIR_INSTALL)
123else  # Empty $(DESTDIR)$(prefix)
124ifeq ($(abs_top_srcdir),$(prefix))
125BUILD_TREE_STATE		:= $(BUILD_TREE_SRCDIR_INSTALL)
126endif
127# END srcdir build-tree install checks
128endif
129# END out-of-build-tree check.
130endif
131
132# Is the build-tree configured yet?
133ifeq ($(BUILD_TREE_STATE),)
134ifneq ($(wildcard $(abs_top_builddir)/include/mk/config.mk),)
135BUILD_TREE_STATE		:= $(BUILD_TREE_NONSRCDIR_INSTALL)
136endif
137endif
138
139ifeq ($(MAKE_3_80_COMPAT),1)
140# Trick make 3.80 into thinking that the default goal is all.
141.PHONY: default
142default: all
143else
144.DEFAULT_GOAL			:= all
145endif
146
147endif	# END autotools, *clean...
148
149BUILD_TREE_STATE		?= $(BUILD_TREE_UNCONFIGURED)
150
151# We can piece together where we're located in the source and object trees with
152# just these two vars and $(CURDIR).
153export abs_top_srcdir abs_top_builddir BUILD_TREE_STATE
154
155endif
156