summaryrefslogtreecommitdiff
path: root/contrib/buildsystems/Generators/QMake.pm
blob: ff3b657e610575091e43cccc8c74139166b8d0d2 (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
package Generators::QMake;
require Exporter;

use strict;
use vars qw($VERSION);

our $VERSION = '1.00';
our(@ISA, @EXPORT, @EXPORT_OK, @AVAILABLE);
@ISA = qw(Exporter);

BEGIN {
    push @EXPORT_OK, qw(generate);
}

sub generate {
    my ($git_dir, $out_dir, $rel_dir, %build_structure) = @_;

    my @libs = @{$build_structure{"LIBS"}};
    foreach (@libs) {
        createLibProject($_, $git_dir, $out_dir, $rel_dir, %build_structure);
    }

    my @apps = @{$build_structure{"APPS"}};
    foreach (@apps) {
        createAppProject($_, $git_dir, $out_dir, $rel_dir, %build_structure);
    }

    createGlueProject($git_dir, $out_dir, $rel_dir, %build_structure);
    return 0;
}

sub createLibProject {
    my ($libname, $git_dir, $out_dir, $rel_dir, %build_structure) = @_;
    print "Generate $libname lib project\n";
    $rel_dir = "../$rel_dir";

    my $sources = join(" \\\n\t", sort(map("$rel_dir/$_", @{$build_structure{"LIBS_${libname}_SOURCES"}})));
    my $defines = join(" \\\n\t", sort(@{$build_structure{"LIBS_${libname}_DEFINES"}}));
    my $includes= join(" \\\n\t", sort(map("$rel_dir/$_", @{$build_structure{"LIBS_${libname}_INCLUDES"}})));
    my $cflags  = join(" ", sort(@{$build_structure{"LIBS_${libname}_CFLAGS"}}));

    my $cflags_debug = $cflags;
    $cflags_debug =~ s/-MT/-MTd/;
    $cflags_debug =~ s/-O.//;

    my $cflags_release = $cflags;
    $cflags_release =~ s/-MTd/-MT/;

    my @tmp  = @{$build_structure{"LIBS_${libname}_LFLAGS"}};
    my @tmp2 = ();
    foreach (@tmp) {
        if (/^-LTCG/) {
        } elsif (/^-L/) {
            $_ =~ s/^-L/-LIBPATH:$rel_dir\//;
        }
        push(@tmp2, $_);
    }
    my $lflags = join(" ", sort(@tmp));

    my $target = $libname;
    $target =~ s/\//_/g;
    $defines =~ s/-D//g;
    $defines =~ s/"/\\\\"/g;
    $includes =~ s/-I//g;
    mkdir "$target" || die "Could not create the directory $target for lib project!\n";
    open F, ">$target/$target.pro" || die "Could not open $target/$target.pro for writing!\n";
    print F << "EOM";
TEMPLATE = lib
TARGET = $target
DESTDIR = $rel_dir

CONFIG -= qt
CONFIG += static

QMAKE_CFLAGS =
QMAKE_CFLAGS_RELEASE = $cflags_release
QMAKE_CFLAGS_DEBUG = $cflags_debug
QMAKE_LIBFLAGS = $lflags

DEFINES += \\
        $defines

INCLUDEPATH += \\
        $includes

SOURCES += \\
        $sources
EOM
    close F;
}

sub createAppProject {
    my ($appname, $git_dir, $out_dir, $rel_dir, %build_structure) = @_;
    print "Generate $appname app project\n";
    $rel_dir = "../$rel_dir";

    my $sources = join(" \\\n\t", sort(map("$rel_dir/$_", @{$build_structure{"APPS_${appname}_SOURCES"}})));
    my $defines = join(" \\\n\t", sort(@{$build_structure{"APPS_${appname}_DEFINES"}}));
    my $includes= join(" \\\n\t", sort(map("$rel_dir/$_", @{$build_structure{"APPS_${appname}_INCLUDES"}})));
    my $cflags  = join(" ", sort(@{$build_structure{"APPS_${appname}_CFLAGS"}}));

    my $cflags_debug = $cflags;
    $cflags_debug =~ s/-MT/-MTd/;
    $cflags_debug =~ s/-O.//;

    my $cflags_release = $cflags;
    $cflags_release =~ s/-MTd/-MT/;

    my $libs;
    foreach (sort(@{$build_structure{"APPS_${appname}_LIBS"}})) {
        $_ =~ s/\//_/g;
        $libs .= " $_";
    }
    my @tmp  = @{$build_structure{"APPS_${appname}_LFLAGS"}};
    my @tmp2 = ();
    foreach (@tmp) {
        # next if ($_ eq "-NODEFAULTLIB:MSVCRT.lib");
        if (/^-LTCG/) {
        } elsif (/^-L/) {
            $_ =~ s/^-L/-LIBPATH:$rel_dir\//;
        }
        push(@tmp2, $_);
    }
    my $lflags = join(" ", sort(@tmp));

    my $target = $appname;
    $target =~ s/\.exe//;
    $target =~ s/\//_/g;
    $defines =~ s/-D//g;
    $defines =~ s/"/\\\\"/g;
    $includes =~ s/-I//g;
    mkdir "$target" || die "Could not create the directory $target for app project!\n";
    open F, ">$target/$target.pro" || die "Could not open $target/$target.pro for writing!\n";
    print F << "EOM";
TEMPLATE = app
TARGET = $target
DESTDIR = $rel_dir

CONFIG -= qt embed_manifest_exe
CONFIG += console

QMAKE_CFLAGS =
QMAKE_CFLAGS_RELEASE = $cflags_release
QMAKE_CFLAGS_DEBUG = $cflags_debug
QMAKE_LFLAGS = $lflags
LIBS   = $libs

DEFINES += \\
        $defines

INCLUDEPATH += \\
        $includes

win32:QMAKE_LFLAGS += -LIBPATH:$rel_dir
else: QMAKE_LFLAGS += -L$rel_dir

SOURCES += \\
        $sources
EOM
    close F;
}

sub createGlueProject {
    my ($git_dir, $out_dir, $rel_dir, %build_structure) = @_;
    my $libs = join(" \\ \n", map("\t$_|$_.pro", @{$build_structure{"LIBS"}}));
    my $apps = join(" \\ \n", map("\t$_|$_.pro", @{$build_structure{"APPS"}}));
    $libs =~ s/\.a//g;
    $libs =~ s/\//_/g;
    $libs =~ s/\|/\//g;
    $apps =~ s/\.exe//g;
    $apps =~ s/\//_/g;
    $apps =~ s/\|/\//g;

    my $filename = $out_dir;
    $filename =~ s/.*\/([^\/]+)$/$1/;
    $filename =~ s/\/$//;
    print "Generate glue project $filename.pro\n";
    open F, ">$filename.pro" || die "Could not open $filename.pro for writing!\n";
    print F << "EOM";
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS += \\
$libs \\
$apps
EOM
    close F;
}

1;