sspt-autogen 11 KB
Newer Older
PoroCYon's avatar
PoroCYon committed
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#!/usr/bin/env bash

print_help() {
    (>&2 cat <<EOF
    Usage: $0 [-h|-?|--help]
               Print this help text and exit.
         - $0 <package-name>
               Generate the .sspt file for this package.
EOF
)
}

autogen_git() {
    cat >> ".sspt" <<EOF
sspt_pull() {
    git pull
}
sspt_version() {
    git rev-parse HEAD
}
sspt_prettyver() {
    git describe --long --tags | sed 's/\([^-]*-g\)/r\1/;s/-/./g'
}
EOF
}
autogen_hg() {
    cat >> ".sspt" <<EOF
sspt_pull() {
    hg pull
}
sspt_version() {
    hg identify -i
}
sspt_prettyver() {
    printf "r%s.%s" "\$(hg identify -n)" "\$(hg identify -i)"
}
EOF
}
autogen_svn() {
    cat >> ".sspt" <<EOF
sspt_pull() {
    svn up
}
sspt_version() {
    svnversion
}
sspt_prettyver() {
    local ver="\$(svnversion)"
    printf "r%s" "\${ver//[[:alpha:]]}"
}
EOF
}
autogen_cvs() {
    # CVS doesn't have 'global' commits...
    cat >> ".sspt" <<EOF
sspt_pull() {
    cvs update
}
sspt_version() {
    date +%Y%m%d
}
sspt_prettyver() {
    date +%Y%m%d
}
EOF
}
autogen_bzr() {
    cat >> ".sspt" <<EOF
sspt_pull() {
    bzr update
}
sspt_version() {
    bzr revno
}
sspt_prettyver() {
    printf "r%s" "\$(bzr revno)"
}
EOF
}
autogen_mtn() {
    cat >> ".sspt" <<EOF
sspt_pull() {
    mtn pull
}
sspt_version() {
    mtn log --last=1 --no-graph | grep "Revision" | egrep -o "[A-Za-z0-9]$"
}
sspt_prettyver() {
    mtn log --last=1 --no-graph | grep "Revision" | egrep -o "[A-Za-z0-9]$"
}
EOF
}
autogen_darcs() {
    cat >> ".sspt" <<EOF
sspt_pull() {
    darcs pull
}
sspt_version() {
    darcs log -n 1 | head -1 | egrep -o "[A-Za-z0-9]$"
}
sspt_prettyver() {
    darcs log -n 1 | head -1 | egrep -o "[A-Za-z0-9]$"
}
EOF
}
autogen_vcs() {
    if [ -d ".git/" ]; then
        autogen_git
    elif [ -d ".hg/" ]; then
        autogen_hg
    elif [ -d ".svn/" ]; then
        autogen_svn
    elif [ -d "CVSROOT/" ]; then
        autogen_cvs
    elif [ -d ".bzr/" ]; then
        autogen_bzr
    elif [ -d "_MTN/" ]; then
        autogen_mtn
    elif [ -d "_darcs/" ]; then
        autogen_darcs
    else
        # no upstream?
        cat >> ".sspt" <<EOF
sspt_pull() {
    true
}
sspt_version() {
    date +%Y%m%d
}
sspt_prettyver() {
    date +%Y%m%d
}
EOF
    fi
}
autogen_buildsys() {
    local REL_DIR="."
    local RELI_DIR="."
    local SSPT_FILE=".sspt"
    local CHANGED_DIR=""
    if [ -z "$DONT_LOOK_INTO_BUILD" ] && [ -d "build" ]; then
        pushd "build" >/dev/null
        CHANGED_DIR=1
        SSPT_FILE="../$SSPT_FILE"
        REL_DIR="./build"
        RELI_DIR="../"
    fi

    if [ -f "package.json" ]; then
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    (>&2 echo "NOTE: npm doesn't honor \$SSPT_DEST_DIR")
}
sspt_build() {
    true
}
sspt_install() {
    npm install
}
sspt_clean() {
    true
}
sspt_clean_config() {
    rm -rf "$REL_DIR/node_packages"
}
EOF
    elif [ -f "Cargo.toml" ]; then
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    (>&2 echo "You may want to edit the .sspt file to enable or disable some features.")
}
sspt_build() {
    cargo build --release
}
sspt_install() {
    cargo install --root \$SSPT_DEST_DIR
}
sspt_clean() {
    cargo clean --release
}
sspt_clean_config() {
    true
}
EOF
    elif [ -f "setup.py" ]; then
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    (>&2 echo "NOTE: sspt assumes that the setup.py script will be executed by the correct python binary.")
    # ensure it is executable
    chmod +x $REL_DIR/setup.py
}
sspt_build() {
    "$REL_DIR/setup.py" build
}
sspt_install() {
    "$REL_DIR/setup.py" install -O2 --prefix \$SSPT_DEST_DIR
}
sspt_clean() {
    "$REL_DIR/setup.py" clean
}
sspt_clean_config() {
    "$REL_DIR/setup.py" clean -a
}
EOF
    elif ! [ -z "$(ls *.ipkg)" ]; then
        local IPKG_FILE=$(ls *.ipkg | head -1) # ensure it's only 1 file
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    (>&2 echo "NOTE: idris doesn't honor \$SSPT_DEST_DIR")
}
sspt_build() {
    idris --build "$REL_DIR/$IPKG_FILE"
}
sspt_install() {
    idris --install "$REL_DIR/$IPKG_FILE" && idris --installdoc "$REL_DIR/$IPKG_FILE"
}
sspt_clean() {
    idris --clean "$REL_DIR/$IPKG_FILE"
}
sspt_clean_config() {
    true
}
EOF
    elif ! [ -z "$(ls *.sln)" ]; then
        local SLN_FILE=$(ls *.sln | head -1)
        local PROBABLE_PROJECT_NAME=${SLN_FILE##*/}
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    (>&2 echo "NOTE: the \`install' command for xbuild projects is extremely hacky, you might want to tweak it.")
}
sspt_build() {
    xbuild /p:Configuration=Release "$REL_DIR/$SLN_FILE"
}
sspt_install() {
    cp "$RELI_DIR/$PROBABLE_PROJECT_NAME/bin/Release/"*.exe \$SSPT_DEST_DIR/bin && \
        cp "$RELI_DIR/$PROBABLE_PROJECT_NAME/bin/Release/"*.dll \$SSPT_DEST_DIR/lib
}
sspt_clean() {
    xbuild /p:Configuration=Release "$REL_DIR/$SLN_FILE" /t:Clean
}
sspt_clean_config() {
    true
}
EOF
    elif [ -f "stack.yaml" ]; then
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    stack build --only-configure
    (>&2 echo "NOTE: stack doesn't honor \$SSPT_DEST_DIR, but installs" \
        "everything to \$XDG_CONFIG_HOME , which is also the default value" \
        "of \$SSPT_DEST_DIR .")
}
sspt_build() {
    stack build --ghc-options -j$(nproc)
}
sspt_install() {
    stack install
}
sspt_clean() {
    stack clean
}
sspt_clean_config() {
    stack clean --full
}
EOF
    elif ! [ -z "$(ls *.cabal)" ]; then
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    cabal configure -O2 --user --prefix=\$SSPT_DEST_DIR --ghc-option=-j$(nproc)
}
sspt_build() {
    cabal build --ghc-option=-j$(nproc)
}
sspt_install() {
    cabal install --prefix=\$SSPT_DEST_DIR
}
sspt_clean() {
    cabal clean -s
}
sspt_clean_config() {
    rm -rf "$REL_DIR/dist"
}
EOF
    elif [ -f "SConstruct" ]; then
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    (>&2 echo "NOTE: SCons cannot install packages.")
}
sspt_build() {
    scons
}
sspt_install() {
    (>&2 echo "NOTE: SCons cannot install packages.")
}
sspt_clean() {
    scons -c
}
sspt_clean_config() {
    true
}
EOF
    elif [ -f "build.gradle" ]; then
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    (>&2 echo "NOTE: Gradle cannot install packages.")
}
sspt_build() {
    grdle -a
}
sspt_install() {
    (>&2 echo "NOTE: Gradle cannot install packages.")
}
sspt_clean() {
    (>&2 echo "NOTE: Gradle cannot clean packages.")
}
sspt_clean_config() {
    true
}
EOF
    elif ! [ -z "$(ls *.sbt)" ]; then
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    (>&2 echo "NOTE: sbt cannot install packages.")
}
sspt_build() {
    sbt
}
sspt_install() {
    (>&2 echo "NOTE: sbt cannot install packages.")
}
sspt_clean() {
    (>&2 echo "NOTE: sbt cannot clean packages.")
}
sspt_clean_config() {
    true
}
EOF
    elif [ -f "BUILD" ]; then
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    (>&2 echo "NOTE: Bazel cannot install packages.")
}
sspt_build() {
    bazel build
}
sspt_install() {
    (>&2 echo "NOTE: Bazel cannot install packages.")
}
sspt_clean() {
    bazel clean
}
sspt_clean_config() {
    true
}
EOF
        (>&2 echo "NOTE: sspt cannot figure out which target to use. Please edit the .sstp file manually.")
        return 1
    elif [ -f "meson.build" ]; then
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    meson --backend ninja --buildtype release --strip --prefix \$SSPT_DEST_DIR "$REL_DIR/meson-build"
}
sspt_build() {
    (>&2 echo "NOTE: meson builds automatically install binaries," \
        "so the project will only be built when installing.")
}
sspt_install() {
    ninja
}
sspt_clean() {
    ninja clean
}
sspt_clean_config() {
    rm -rf "$REL_DIR/meson-build"
}
EOF
    elif [ -f "build.ninja" ]; then
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    (>&2 echo "NOTE: ninja cannot install packages.")
}
sspt_build() {
    ninja
}
sspt_install() {
    (>&2 echo "NOTE: ninja cannot install packages.")
}
sspt_clean() {
    ninja clean
}
sspt_clean_config() {
    true
}
EOF
    elif [ -f "build.xml" ]; then
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    (>&2 echo "NOTE: ant cannot install packages.")
}
sspt_build() {
    ant
}
sspt_install() {
    (>&2 echo "NOTE: ant cannot install packages.")
}
sspt_clean() {
    ant clean
}
sspt_clean_config() {
    true
}
EOF
    elif [ -f "pom.xml" ]; then
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    true
}
sspt_build() {
    mvn -B package
}
sspt_install() {
    mvn -B install
}
sspt_clean() {
    mvn -B clean
}
sspt_clean_config() {
    true
}
EOF
    elif [ -f "CMakeLists.txt" ]; then
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    mkdir -p "$REL_DIR/cmake-build"
    pushd "$REL_DIR/cmake-build" >/dev/null
    cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=\$SSPT_DEST_DIR
    popd >/dev/null
}
sspt_build() {
    pushd "$REL_DIR/cmake-build" >/dev/null
    make -j$(nproc)
    popd >/dev/null
}
sspt_install() {
    pushd "$REL_DIR/cmake-build" >/dev/null
    make install
    popd >/dev/null
}
sspt_clean() {
    pushd "$REL_DIR/cmake-build" >/dev/null
    make clean
    popd >/dev/null
}
sspt_clean_config() {
    rm -rf "$REL_DIR/cmake-build"
}
EOF
    elif ! [ -z "$(ls *.pro)" ]; then
        local PRO_FILE="$(ls *.pro | head -1)"

        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    qmake -makefile "$REL_DIR/$PRO_FILE"
}
sspt_build() {
    make -j$(nproc)
}
sspt_install() {
    make install PREFIX=\$SSPT_DEST_DIR prefix=\$SSPT_DEST_DIR
}
sspt_clean() {
    make clean
}
sspt_clean_config() {
    rm "$REL_DIR/*akefile"
}
EOF
    elif [ -f "./configure" ]; then
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    "$REL_DIR/configure" --prefix=\$SSPT_DEST_DIR
}
sspt_build() {
    make -j$(nproc)
}
sspt_install() {
    make install
}
sspt_clean() {
    make clean
}
sspt_clean_config() {
    rm -rf "$REL_DIR/"*akefile "$REL_DIR/"config.*
}
EOF
    elif [ -f "Makefile" ]; then
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    true
}
sspt_build() {
    make -j$(nproc)
}
sspt_install() {
    make install PREFIX=\$SSPT_DEST_DIR prefix=\$SSPT_DEST_DIR
}
sspt_clean() {
    make clean
}
sspt_clean_config() {
    true
}
EOF
    elif ! [ -z "$CHANGED_DIR" ]; then
        # try top directory instead
        popd >/dev/null
        DONT_LOOK_INTO_BUILD=1 autogen_buildsys
        return $?
    else
        (>&2 echo "Could not detect how to compile the package. ")
        return 1
    fi

    if ! [ -z "$CHANGED_DIR" ];
        popd >/dev/null
    fi
}

autogen_cwd() {
    touch ".sspt"
    #echo "name=\"$(basename `pwd`)\"" >> ".sspt"
    autogen_vcs
    autogen_buildsys
    return $?
}

main() {
    local ESCAPED_PKG=`escape_string $1`

    case $1 in
        -h|-?|--help)
            print_help
            ;;
        *)
            egrep -q -e "^$ESCAPED_PKG " -f "$SSPT_SRC_DB_FILE"
            if [ $? -ne 0 ]; then
                (>&2 echo "Package '$1' doesn't exist.")
                exit 1
            fi

            local PACKAGE_DIR=$(egrep -e "^$ESCAPED_PKG " -f "$SSPT_SRC_DB_FILE" \
                | egrep -v -o -e "^[A-Za-z0-9_\-\.]* " | egrep -v -o -e " [A-Za-z0-9]$")

            pushd "$PACKAGE_DIR" >/dev/null
            autogen_cwd
            local RETVAL=$?
            popd >/dev/null
            exit $RETVAL
            ;;
    esac
}

if [ $# -eq 0 ]; then
    print_help
    exit
fi

main $@