1#
2#    Copyright 2015-2016 Nest Labs Inc. All Rights Reserved.
3#
4#    Licensed under the Apache License, Version 2.0 (the "License");
5#    you may not use this file except in compliance with the License.
6#    You may obtain a copy of the License at
7#
8#    http://www.apache.org/licenses/LICENSE-2.0
9#
10#    Unless required by applicable law or agreed to in writing, software
11#    distributed under the License is distributed on an "AS IS" BASIS,
12#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13#    See the License for the specific language governing permissions and
14#    limitations under the License.
15#
16
17#
18#    Description:
19#      This file defines a GNU autoconf M4-style macro that adds an
20#      --enable-optimization configuration option to the package and
21#      controls whether the package will be built with or without code
22#      optimization.
23#
24
25#
26# NL_ENABLE_OPTIMIZATION(default)
27#
28#   default - Whether the option should be enabled (yes) or disabled (no)
29#             by default.
30#
31# Adds an --enable-optimization configuration option to the package with a
32# default value of 'default' (should be either 'no' or 'yes') and controls
33# whether the package will be built with or without code optimization.
34#
35# The value 'nl_cv_build_optimized' will be set to the result. In
36# addition, the contents of CFLAGS, CXXFLAGS, OBJCFLAGS, and OBJCXXFLAGS may
37# be altered by the use of this macro, converting -O<something> to -O0.
38#
39# NOTE: The behavior of this is influenced by nl_cv_build_coverage from
40#       NL_ENABLE_COVERAGE
41#
42#------------------------------------------------------------------------------
43AC_DEFUN([NL_ENABLE_OPTIMIZATION],
44[
45    # Check whether or not a default value has been passed in.
46
47    m4_case([$1],
48        [yes],[],
49        [no],[],
50        [m4_fatal([$0: invalid default value '$1'; must be 'yes' or 'no'])])
51
52    AC_CACHE_CHECK([whether to build code-optimized instances of programs and libraries],
53        nl_cv_build_optimized,
54        [
55            AC_ARG_ENABLE(optimization,
56                [AS_HELP_STRING([--enable-optimization],[Enable the generation of code-optimized instances @<:@default=$1@:>@.])],
57                [
58                    case "${enableval}" in
59
60                    no|yes)
61                        nl_cv_build_optimized=${enableval}
62
63                        if test "${nl_cv_build_coverage}" = "yes" && test "${nl_cv_build_optimized}" = "yes"; then
64                            AC_MSG_ERROR([both --enable-optimization and --enable-coverage cannot used. Please, choose one or the other to enable.])
65                        fi
66                        ;;
67
68                    *)
69                        AC_MSG_ERROR([Invalid value ${enableval} for --enable-optimized])
70                        ;;
71
72                    esac
73                ],
74                [
75                    if test "${nl_cv_build_coverage}" = "yes"; then
76                        AC_MSG_WARN([--enable-coverage was specified, optimization disabled])
77                        nl_cv_build_optimized=no
78
79                    else
80                        nl_cv_build_optimized=$1
81
82                    fi
83                ])
84
85            if test "${nl_cv_build_optimized}" = "no"; then
86                CFLAGS="`echo ${CFLAGS} | sed -e 's,-O[[[:alnum:]]]*,-O0,g'`"
87                CXXFLAGS="`echo ${CXXFLAGS} | sed -e 's,-O[[[:alnum:]]]*,-O0,g'`"
88                OBJCFLAGS="`echo ${OBJCFLAGS} | sed -e 's,-O[[[:alnum:]]]*,-O0,g'`"
89                OBJCXXFLAGS="`echo ${OBJCXXFLAGS} | sed -e 's,-O[[[:alnum:]]]*,-O0,g'`"
90            fi
91    ])
92])
93