/
cmake-project
executable file
·95 lines (80 loc) · 2.18 KB
/
cmake-project
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
#!/bin/sh
# SPDX-FileCopyrightText: 2020 Klaravdalens Datakonsult AB (KDAB), author David Faure <david.faure@kdab.com>
# SPDX-License-Identifier: MIT
# Generate a CMakeLists.txt based on the .cpp files in the current directory
# Similar to qmake -project, in essence.
usage()
{
echo "Usage: $0 [ -q | --qt 5 | 6 | 5-and-6 | none ]"
echo "The -q or --qt option allows to choose between Qt 5, Qt 6, both, or no Qt dependency"
exit 2
}
PARSED_ARGUMENTS=$(getopt -a -n cmake-project -o q:,h --long qt:,help -- "$@")
INVALID_ARGUMENTS=$?
if [ "$INVALID_ARGUMENTS" != "0" ]; then
usage
fi
eval set -- "$PARSED_ARGUMENTS"
QTVERSION=5
while :
do
case "$1" in
-q | --qt) QTVERSION="$2" ; shift 2 ;;
-h | --help) usage; ;;
--) shift; break;;
*) echo "Unexpected option: $1"; usage; ;; # can't happen
esac
done
if [ -f CMakeLists.txt ]; then
echo "CMakeLists.txt already exists, aborting for safety reasons"
exit 1
fi
FILES="`find -name '*.cpp' -o -name '*.qrc' -o -name '*.ui' | sed -e 's,\.\/, ,' | sort -V`"
if [ -z "$FILES" ]; then
echo "No cpp files found in the current directory"
exit 1
fi
PROJECTNAME=`basename "$PWD" | sed -e 's/ /_/g'`
TARGET="$PROJECTNAME"
if [ "$QTVERSION" != "none" ]; then
if [ "$QTVERSION" = "5-and-6" ]; then
QT="Qt\${QT_VERSION_MAJOR}"
else
QT=Qt$QTVERSION
fi
QTLIBS="Core Gui"
QTTARGETS="$QT::Core $QT::Gui"
if [ -n "`grep QApplication *.cpp`" ]; then
QTLIBS="$QTLIBS Widgets"
QTTARGETS="$QTTARGETS $QT::Widgets"
AUTOUIC="set(CMAKE_AUTOUIC TRUE)"
fi
fi
cat > CMakeLists.txt <<EOF
cmake_minimum_required(VERSION 3.9)
project($PROJECTNAME)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
EOF
if [ "$QTVERSION" = "5-and-6" ]; then
cat >> CMakeLists.txt <<EOF
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS $QTLIBS)
EOF
fi
if [ "$QTVERSION" != "none" ]; then
cat >> CMakeLists.txt <<EOF
find_package($QT CONFIG REQUIRED COMPONENTS $QTLIBS)
set(CMAKE_AUTOMOC TRUE)
set(CMAKE_AUTORCC TRUE)
$AUTOUIC
EOF
fi
cat >> CMakeLists.txt <<EOF
add_executable($PROJECTNAME
$FILES
)
EOF
if [ "$QTVERSION" != "none" ]; then
cat >> CMakeLists.txt <<EOF
target_link_libraries($PROJECTNAME $QTTARGETS)
EOF
fi