summaryrefslogtreecommitdiffstats
path: root/arduino-cmake/configure
blob: ca7c4c31937816d5efe368a7a3afac52be565c59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env bash

#==============================================================================#
#                                                                              #
#  Author     : QueezyTheGreat                                                 #
#  Description: Build configuration script                                     #
#                                                                              #
#    Makes sure that we don't execute cmake or ccmake in the source            #
#  directory(we like prestine sources)                                         #
#                                                                              #
#    If we execute this script in the sources directory, a build directory     #
#  will be created from where the actual build configuration will be executed. #
#                                                                              #
#    If we aren't in the source directory everything works normally.           #
#                                                                              #
#    By default cmake is used to configure the build system, but you can also  #
#  chose from the ncurses version (ccmake) and the GUI version (cmake-gui).    #
#                                                                              #
#==============================================================================#


# Find executable path
function path_locate {
    local EXECUTABLE_NAME
    local PATH_OUTPUT_VARIABLE_NAME
    local EXECUTABLE_PATH

    # Check if we have required arguments
    if [ "${#}" -lt 2 ]; then
        echo "Insuficient amount of arguments..."
        echo "locate_path EXECUTABLE_NAME PATH_OUTPUT_VARIABLE_NAME"
        exit 1
    fi

    EXECUTABLE_NAME="$1"
    PATH_OUTPUT_VAR="$2"

    if [ "${EXECUTABLE_NAME}X" = "X" ]; then
        echo "Missing executable name"
        echo "locate_path EXECUTABLE_NAME PATH_OUTPUT_VARIABLE_NAME"
        exit 1
    fi
    if [ "${PATH_OUTPUT_VAR}X" = "X" ]; then
        echo "Missing path output variable name"
        echo "locate_path EXECUTABLE_NAME PATH_OUTPUT_VARIABLE_NAME"
        exit 1
    fi

    # Try to find full path
    EXECUTABLE_PATH="$(type -p ${EXECUTABLE_NAME})"

    # Setting executable path
    eval "${PATH_OUTPUT_VAR}"=${EXECUTABLE_PATH}
}

# Find executable path, exit if not found
function require {
    local EXECUTABLE_NAME
    local PATH_OUTPUT_VARIABLE_NAME
    local EXECUTABLE_PATH

    # Check if we have required arguments
    if [ "${#}" -lt 2 ]; then
        echo "Insuficient amount of arguments..."
        echo "require EXECUTABLE_NAME PATH_OUTPUT_VARIABLE_NAME"
        exit 1
    fi

    EXECUTABLE_NAME="$1"
    PATH_OUTPUT_VAR="$2"

    if [ "${EXECUTABLE_NAME}X" = "X" ]; then
        echo "Missing executable name"
        echo "require EXECUTABLE_NAME PATH_OUTPUT_VARIABLE_NAME"
        exit 1
    fi
    if [ "${PATH_OUTPUT_VAR}X" = "X" ]; then
        echo "Missing path output variable name"
        echo "require EXECUTABLE_NAME PATH_OUTPUT_VARIABLE_NAME"
        exit 1
    fi

    # Try to find full path
    EXECUTABLE_PATH="$(type -p ${EXECUTABLE_NAME})"

    if [ "${EXECUTABLE_PATH}X" = "X" ]; then
        # No path was found.... failing
        echo "${EXECUTABLE_NAME} is not available"
        echo "Aborting..."

        exit -1
    fi

    if [ ! -x "${EXECUTABLE_PATH}" ]; then
        echo "${EXECUTABLE_NAME} is not executable"
        echo "Aborting..."

        exit -1
    fi

    # Setting executable path
    eval "${PATH_OUTPUT_VAR}"=${EXECUTABLE_PATH}
}


# Executable names
CMAKE_NAME="cmake"
CCMAKE_NAME="ccmake"
CMAKEGUI_NAME="cmake-gui"

# Find executable paths
require     ${CMAKE_NAME} CMAKE_PATH
path_locate ${CCMAKE_NAME} CCMAKE_PATH
path_locate ${CMAKEGUI_NAME} CMAKEGUI_PATH

# Warning file
WARNING_PATH="${HOME}/.configure_warning"

# Detect where we are and where the sources are
CONFIG_BASE_PATH=$(cd "$(dirname "$0")"; pwd)
CURRENT_PATH=$(pwd)

IN_BASE="$(echo ${CURRENT_PATH}| grep ${CONFIG_BASE_PATH})"
IN_BUILD="$(echo ${CURRENT_PATH}| grep ${CONFIG_BASE_PATH}/build)"

# Set the default cmake executable
# to be used
CMAKE_EXECUTABLE="${CMAKE_PATH}"

# Check for command line arguments
case $1 in
    "-h")
        echo "$0 [-h|-x|-c] [cmake_args...]"
        echo
        echo "         -h      Display this help message"
        echo "         -x      Run GUI version of CMake"
        echo "         -c      Run Curses version of CMake"
        echo
        echo " cmake_args      CMake arguments that will be passed"
        echo "                 to the chosen CMake binary, by default"
        echo "                 cmake is used (if -x or -c not used)"
        echo
        
        exit 1
        ;;
    "-x")
        if [ ! -e "${CMAKEGUI_PATH}" ]; then
            echo "${CMAKEGUI_NAME} is not available"
            echo "Aborting...'"
            
            exit -1
        fi
        
        # Setting executable to cmake-gui
        CMAKE_EXECUTABLE="${CMAKEGUI_PATH}"
        
        # Shift argument list, getting rid of script name
        # and first command line argument
        shift
        ;;
    "-c")
        if [ ! -e "${CCMAKE_PATH}" ]; then
            echo "${CCMAKE_NAME} is not available"
            echo "Aborting...'"
            
            exit -1

        fi
        
        # Setting executable to ccmake
        CMAKE_EXECUTABLE="${CCMAKE_PATH}"
        
        # Shift argument list, getting rid of script name
        # and first command line argument
        shift
        ;;
esac


echo
echo Project Sources: ${CONFIG_BASE_PATH}
echo CMake Executable: ${CMAKE_EXECUTABLE}
echo


# Check if we are in source directory
if [ "${IN_BASE}X" != "X" ]; then
    # We are within the source directory
    if [ "${IN_BUILD}X" != "X" ]; then
        # We are in the build directory
        echo 'Building within build directory'
        # Running chosen cmake executable
        ${CMAKE_EXECUTABLE} ${*} ${CONFIG_BASE_PATH}
    else
        # We are in the source directory
        # but not within the build directory
        if [ ! -f "${WARNING_PATH}" ]; then
            echo
            echo
            echo "Running CMake within the source directory is HIGHLY discouraged."
            echo "You should either build the project outside the source directory"
            echo "or create a directory called build within the root of the project"
            echo
            echo
            echo "This warning will only apear once..."
            echo 
            echo
            echo
            echo -n "Continuing in 15"
            for COUNT in 14 13 12 11 10 9 8 7 6 5 4 3 2 1; do
                sleep 1
                echo -n "..${COUNT}"
            done
            echo

            # Removing waring
            touch "${WARNING_PATH}"
        fi

        if [ ! -d "${CONFIG_BASE_PATH}/build" ]; then
            echo "Creating build directory: ${CONFIG_BASE_PATH}/build"
            mkdir "${CONFIG_BASE_PATH}/build"
        fi

        # Changing to build directory
        echo "Changing to build directory: ${CONFIG_BASE_PATH}/build"
        cd "${CONFIG_BASE_PATH}/build"

        # Running chosen cmake executable in new build directory
        ${CMAKE_EXECUTABLE} ${*} ${CONFIG_BASE_PATH}

    fi
else
    # We are outside of the source directory
    # Running chosen cmake executable
    ${CMAKE_EXECUTABLE} ${*} ${CONFIG_BASE_PATH}
fi

if [ "${?}" -eq 0 ]; then
    echo 
    echo "To build project:    make"
    echo "To install project:  make install"
    echo "Generate packages:   make package"
    echo

    exit 0
else
    exit 1
fi