sspt-autogen 12.8 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
196
197
sspt_config() {
    abuild prepare
}
sspt_build() {
    abuild build
}
sspt_install() {
    abuild package -i
}
sspt_clean() {
    abuild clean
}
sspt_clean_config() {
    abuild clean
    abuild cleancache
}
EOF
    elif [ -f "package.json" ]; then
PoroCYon's avatar
PoroCYon committed
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
        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
253
254
    elif file_exists_glob ./*.sln; then
        local SLN_FILE=$(find ./*.sln -maxdepth 1 -type f | head -1)
PoroCYon's avatar
PoroCYon committed
255
        local PROBABLE_PROJECT_NAME=${SLN_FILE%.*}
PoroCYon's avatar
PoroCYon committed
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
        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
295
    elif file_exists_glob ./*.cabal; then
PoroCYon's avatar
PoroCYon committed
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
        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
349
    elif file_exists_glob ./*.sbt; then
PoroCYon's avatar
PoroCYon committed
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
        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
PoroCYon's avatar
PoroCYon committed
465
    cmake .. -DCMAKE_BUILD_TYPE=Release "-DCMAKE_INSTALL_PREFIX=\$SSPT_DEST_DIR"
PoroCYon's avatar
PoroCYon committed
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
    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
487
488
    elif file_exists_glob ./*.pro; then
        local PRO_FILE="$(find ./*.pro -maxdepth 1 -type f | head -1)"
PoroCYon's avatar
PoroCYon committed
489
490
491
492
493
494
495
496
497

        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    qmake -makefile "$REL_DIR/$PRO_FILE"
}
sspt_build() {
    make -j$(nproc)
}
sspt_install() {
PoroCYon's avatar
PoroCYon committed
498
    make install "PREFIX=\$SSPT_DEST_DIR" "prefix=\$SSPT_DEST_DIR"
PoroCYon's avatar
PoroCYon committed
499
500
501
502
503
504
505
}
sspt_clean() {
    make clean
}
sspt_clean_config() {
    rm "$REL_DIR/*akefile"
}
PoroCYon's avatar
PoroCYon committed
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
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
}
sspt_clean() {
    make clean
}
sspt_clean_config() {
    rm -rf "$REL_DIR/configure" "$REL_DIR/"*akefile "$REL_DIR/"config.*
}
PoroCYon's avatar
PoroCYon committed
525
526
527
528
EOF
    elif [ -f "./configure" ]; then
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
PoroCYon's avatar
PoroCYon committed
529
    "$REL_DIR/configure" "--prefix=\$SSPT_DEST_DIR"
PoroCYon's avatar
PoroCYon committed
530
531
532
533
534
535
536
537
538
539
540
541
542
543
}
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
544
545
    elif file_exists_glob ./*akefile; then # support GNUmakefile etc.
        local MAKEFILE=$(find ./*akefile -maxdepth 1 -type f | head -1)
PoroCYon's avatar
PoroCYon committed
546
547
548
549
550
        cat >> "$SSPT_FILE" <<EOF
sspt_config() {
    true
}
sspt_build() {
551
    make -f "$MAKEFILE" -j$(nproc)
PoroCYon's avatar
PoroCYon committed
552
553
}
sspt_install() {
PoroCYon's avatar
PoroCYon committed
554
    make -f "$MAKEFILE" install "PREFIX=\$SSPT_DEST_DIR" "prefix=\$SSPT_DEST_DIR" "DESTDIR=\$SSPT_DEST_DIR"
PoroCYon's avatar
PoroCYon committed
555
556
}
sspt_clean() {
557
    make -f "$MAKEFILE" clean
PoroCYon's avatar
PoroCYon committed
558
559
560
561
}
sspt_clean_config() {
    true
}
PoroCYon's avatar
PoroCYon committed
562
EOF
563
564
    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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
        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
}
PoroCYon's avatar
PoroCYon committed
581
582
583
584
585
586
587
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
588
        (>&2 echo "Could not detect how to compile package '$1'.")
PoroCYon's avatar
PoroCYon committed
589
590
591
        return 1
    fi

PoroCYon's avatar
PoroCYon committed
592
    if ! [ -z "$CHANGED_DIR" ]; then
PoroCYon's avatar
PoroCYon committed
593
594
595
596
597
598
599
        popd >/dev/null
    fi
}

autogen_cwd() {
    touch ".sspt"
    #echo "name=\"$(basename `pwd`)\"" >> ".sspt"
PoroCYon's avatar
PoroCYon committed
600
601
    autogen_vcs "$1"
    autogen_buildsys "$1"
PoroCYon's avatar
PoroCYon committed
602
603
604
605
    return $?
}

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

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

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

PoroCYon's avatar
PoroCYon committed
627
            local PACKAGE_DIR="$SSPT_DL_DIR/$ESCAPED_PKG"
PoroCYon's avatar
PoroCYon committed
628
629

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

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

643
main "$@"
PoroCYon's avatar
PoroCYon committed
644