sspt-autogen 12.9 KB
Newer Older
PoroCYon's avatar
PoroCYon committed
1
2
3
4
#!/usr/bin/env bash

print_help() {
    (>&2 cat <<EOF
PoroCYon's avatar
PoroCYon committed
5
Autogenerate an .sspt file for a package.
PoroCYon's avatar
PoroCYon committed
6
Usage: sspt autogen [-h|-?|--help]
PoroCYon's avatar
PoroCYon committed
7
           Print this help text and exit.
PoroCYon's avatar
PoroCYon committed
8
9
10
     - sspt autogen {-e|--edit} <package-name>
           Generate the .sspt file for this package and optionally edit
           it afterwards.
PoroCYon's avatar
PoroCYon committed
11
12
13
14
Supported VCSes: git, hg, svn, cvs, bzr, mtn, darcs
Supported build systems: npm, cargo, setuptools, idris, xbuild, stack,
    cabal, scons, gradle, sbt, bazel, meson, ninja, ant, mvn, cmake,
    qmake, autoconf, makefile
PoroCYon's avatar
PoroCYon committed
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
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
PoroCYon's avatar
PoroCYon committed
90
    mtn update
PoroCYon's avatar
PoroCYon committed
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
}
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
PoroCYon's avatar
PoroCYon committed
129
130
        (>&2 echo "NOTE: could not detect repository type of package" \
            "'$1', add the .sspt file to make updating this pacakge possible.")
PoroCYon's avatar
PoroCYon committed
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
        # 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

PoroCYon's avatar
PoroCYon committed
158
159
    if [ -f "PKGBUILD" ]; then
        cat >> "$SSPT_FILE" <<EOF
160
export SSPT_TRACK_FILES="no"
PoroCYon's avatar
PoroCYon committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
sspt_config() {
    makepkg -o
}
sspt_build() {
    makepkg -e
}
sspt_install() {
    makepkg -e -o -i
}
sspt_clean() {
    makepkg -c -e -o
}
sspt_clean_config() {
    true
}
EOF
    elif [ -f "APKBUILD" ]; then
        cat >> "$SSPT_FILE" <<EOF
179
export SSPT_TRACK_FILES="no"
PoroCYon's avatar
PoroCYon committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
sspt_config() {
    abuild prepare
}
sspt_build() {
    abuild build
}
sspt_install() {
    abuild package -i
}
sspt_clean() {
    abuild clean
}
sspt_clean_config() {
    abuild clean
    abuild cleancache
}
PoroCYon's avatar
PoroCYon committed
196
197
198
199
200
201
202
203
204
205
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() {
PoroCYon's avatar
PoroCYon committed
206
    make -C "$REL_DIR/cmake-build" -j$(nproc)
PoroCYon's avatar
PoroCYon committed
207
208
}
sspt_install() {
PoroCYon's avatar
PoroCYon committed
209
    make -C "$REL_DIR/cmake-build" install
PoroCYon's avatar
PoroCYon committed
210
211
}
sspt_clean() {
PoroCYon's avatar
PoroCYon committed
212
    make -C "$REL_DIR/cmake-build" clean
PoroCYon's avatar
PoroCYon committed
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
}
sspt_clean_config() {
    rm -rf "$REL_DIR/cmake-build"
}
EOF
    elif [ -f "./configure.ac" ]; then
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    autoreconf -i
    "$REL_DIR/configure" "--prefix=\$SSPT_DEST_DIR"
}
sspt_build() {
    make -j$(nproc)
}
sspt_install() {
    make install "PREFIX=\$SSPT_DEST_DIR" "prefix=\$SSPT_DEST_DIR" "DESTDIR=\$SSPT_DEST_DIR"
}
sspt_clean() {
    make clean
}
sspt_clean_config() {
    rm -rf "$REL_DIR/configure" "$REL_DIR/"*akefile "$REL_DIR/"config.*
}
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 "PREFIX=\$SSPT_DEST_DIR" "prefix=\$SSPT_DEST_DIR" "DESTDIR=\$SSPT_DEST_DIR"
}
sspt_clean() {
    make clean
}
sspt_clean_config() {
    rm -rf "$REL_DIR/"*akefile "$REL_DIR/"config.*
}
EOF
    elif file_exists_glob ./*.pro; then
        local PRO_FILE="$(find ./*.pro -maxdepth 1 -type f | 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" "DESTDIR=\$SSPT_DEST_DIR"
}
sspt_clean() {
    make clean
}
sspt_clean_config() {
    rm "$REL_DIR/*akefile"
}
EOF
    elif file_exists_glob ./*akefile; then # support GNUmakefile etc.
        local MAKEFILE=$(find ./*akefile -maxdepth 1 -type f | head -1)
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    true
}
sspt_build() {
    make -f "$MAKEFILE" -j$(nproc)
}
sspt_install() {
    make -f "$MAKEFILE" install "PREFIX=\$SSPT_DEST_DIR" "prefix=\$SSPT_DEST_DIR" "DESTDIR=\$SSPT_DEST_DIR"
}
sspt_clean() {
    make -f "$MAKEFILE" clean
}
sspt_clean_config() {
    true
}
PoroCYon's avatar
PoroCYon committed
293
294
EOF
    elif [ -f "package.json" ]; then
PoroCYon's avatar
PoroCYon committed
295
296
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
PoroCYon's avatar
PoroCYon committed
297
    >&2 echo "NOTE: npm doesn't honor \$SSPT_DEST_DIR"
PoroCYon's avatar
PoroCYon committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
}
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() {
PoroCYon's avatar
PoroCYon committed
315
    >&2 echo "You may want to edit the .sspt file to enable or disable some features."
PoroCYon's avatar
PoroCYon committed
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
}
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() {
PoroCYon's avatar
PoroCYon committed
333
    >&2 echo "NOTE: sspt assumes that the setup.py script will be executed by the correct python binary."
PoroCYon's avatar
PoroCYon committed
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
    # 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 [ -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
371
    elif file_exists_glob ./*.cabal; then
PoroCYon's avatar
PoroCYon committed
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
        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
425
    elif file_exists_glob ./*.sbt; then
PoroCYon's avatar
PoroCYon committed
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
        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
PoroCYon's avatar
PoroCYon committed
536
537
    elif file_exists_glob ./*.ipkg; then
        local IPKG_FILE=$(find ./*.ipkg -maxdepth 1 -type f | head -1) # ensure it's only 1 file
PoroCYon's avatar
PoroCYon committed
538
539
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
PoroCYon's avatar
PoroCYon committed
540
    (>&2 echo "NOTE: idris doesn't honor \$SSPT_DEST_DIR")
PoroCYon's avatar
PoroCYon committed
541
542
}
sspt_build() {
PoroCYon's avatar
PoroCYon committed
543
    idris --build "$REL_DIR/$IPKG_FILE"
PoroCYon's avatar
PoroCYon committed
544
545
}
sspt_install() {
PoroCYon's avatar
PoroCYon committed
546
    idris --install "$REL_DIR/$IPKG_FILE" && idris --installdoc "$REL_DIR/$IPKG_FILE"
PoroCYon's avatar
PoroCYon committed
547
548
}
sspt_clean() {
PoroCYon's avatar
PoroCYon committed
549
    idris --clean "$REL_DIR/$IPKG_FILE"
PoroCYon's avatar
PoroCYon committed
550
551
552
553
}
sspt_clean_config() {
    true
}
PoroCYon's avatar
PoroCYon committed
554
EOF
PoroCYon's avatar
PoroCYon committed
555
556
557
    elif file_exists_glob ./*.sln; then
        local SLN_FILE=$(find ./*.sln -maxdepth 1 -type f | head -1)
        local PROBABLE_PROJECT_NAME=${SLN_FILE%.*}
PoroCYon's avatar
PoroCYon committed
558
559
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
PoroCYon's avatar
PoroCYon committed
560
    >&2 echo "NOTE: the \`install' command for xbuild projects is extremely hacky, you might want to tweak it."
PoroCYon's avatar
PoroCYon committed
561
562
}
sspt_build() {
PoroCYon's avatar
PoroCYon committed
563
    xbuild /p:Configuration=Release "$REL_DIR/$SLN_FILE"
PoroCYon's avatar
PoroCYon committed
564
565
}
sspt_install() {
PoroCYon's avatar
PoroCYon committed
566
567
    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
PoroCYon's avatar
PoroCYon committed
568
569
}
sspt_clean() {
PoroCYon's avatar
PoroCYon committed
570
    xbuild /p:Configuration=Release "$REL_DIR/$SLN_FILE" /t:Clean
PoroCYon's avatar
PoroCYon committed
571
572
573
574
}
sspt_clean_config() {
    true
}
PoroCYon's avatar
PoroCYon committed
575
576
577
578
579
580
581
EOF
    elif ! [ -z "$CHANGED_DIR" ]; then
        # try top directory instead
        popd >/dev/null
        DONT_LOOK_INTO_BUILD=1 autogen_buildsys
        return $?
    else
PoroCYon's avatar
PoroCYon committed
582
        (>&2 echo "Could not detect how to compile package '$1'.")
PoroCYon's avatar
PoroCYon committed
583
584
585
        return 1
    fi

PoroCYon's avatar
PoroCYon committed
586
    if ! [ -z "$CHANGED_DIR" ]; then
PoroCYon's avatar
PoroCYon committed
587
588
589
590
591
        popd >/dev/null
    fi
}

autogen_cwd() {
PoroCYon's avatar
PoroCYon committed
592
593
594
    if [ -f ".sspt" ]; then
        rm ".sspt"
    fi
PoroCYon's avatar
PoroCYon committed
595
596
    touch ".sspt"
    #echo "name=\"$(basename `pwd`)\"" >> ".sspt"
PoroCYon's avatar
PoroCYon committed
597
598
    autogen_vcs "$1"
    autogen_buildsys "$1"
PoroCYon's avatar
PoroCYon committed
599
600
601
602
    return $?
}

main() {
603
    local ESCAPED_PKG=$(escape_string "$1")
PoroCYon's avatar
PoroCYon committed
604

PoroCYon's avatar
fix _    
PoroCYon committed
605
606
    case "$1" in
        "-h"|"-?"|"--help")
PoroCYon's avatar
PoroCYon committed
607
608
            print_help
            ;;
PoroCYon's avatar
fix _    
PoroCYon committed
609
        "-e"|"--edit")
PoroCYon's avatar
PoroCYon committed
610
611
612
613
614
615
616
617
            if [ -z "$2" ]; then
                print_help
                exit
            fi

            main "$2"
            exec_subcommand edit "$2"
            ;;
PoroCYon's avatar
PoroCYon committed
618
        *)
619
            if ! exec_subcommand src-db exists "$1"; then
PoroCYon's avatar
PoroCYon committed
620
621
622
623
                (>&2 echo "Package '$1' doesn't exist.")
                exit 1
            fi

PoroCYon's avatar
PoroCYon committed
624
            local PACKAGE_DIR="$SSPT_DL_DIR/$ESCAPED_PKG"
PoroCYon's avatar
PoroCYon committed
625
626

            pushd "$PACKAGE_DIR" >/dev/null
PoroCYon's avatar
PoroCYon committed
627
            autogen_cwd "$1"
PoroCYon's avatar
PoroCYon committed
628
629
630
631
632
633
634
635
636
637
638
639
            local RETVAL=$?
            popd >/dev/null
            exit $RETVAL
            ;;
    esac
}

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

640
main "$@"
PoroCYon's avatar
PoroCYon committed
641