1/*
2   --- To sync with upstream:
3
4    # Update source and regenerate generated files.
5    git remote add toybox https://github.com/landley/toybox.git
6    git fetch toybox && git merge toybox/master && ./regenerate.sh
7
8    # Make any necessary Android.bp changes and rebuild.
9    mm -j32
10
11    # Run all the tests.
12    ./run-tests-on-android.sh
13    # Run a single test.
14    ./run-tests-on-android.sh wc
15
16    # Upload changes.
17    git commit -a --amend
18    git push aosp HEAD:refs/for/master  # Push to gerrit for review.
19    git push aosp HEAD:master           # Push directly, avoiding gerrit.
20
21
22   --- To add a toy:
23
24    # Edit the three .config-* files to enable the toy you want for the targets
25    # you want it on, and regenerate the generated files:
26    ./regenerate.sh
27
28    # Edit the relevant `srcs` below, depending on where the toy should be
29    # available.
30
31    # If you just want to use the toy via "toybox x" rather than "x", you can
32    # stop now. If you want this toy to have a symbolic link in /system/bin,
33    # add the toy to symlinks.
34
35*/
36
37package {
38    default_applicable_licenses: ["external_toybox_license"],
39}
40
41license {
42    name: "external_toybox_license",
43    visibility: [":__subpackages__"],
44    license_kinds: [
45        "SPDX-license-identifier-0BSD",
46        "SPDX-license-identifier-CC0-1.0",
47        "SPDX-license-identifier-Unlicense",
48        "legacy_unencumbered",
49    ],
50    license_text: [
51        "LICENSE",
52    ],
53}
54
55all_srcs = [
56    "lib/args.c",
57    "lib/commas.c",
58    "lib/dirtree.c",
59    "lib/env.c",
60    "lib/help.c",
61    "lib/lib.c",
62    "lib/linestack.c",
63    "lib/llist.c",
64    "lib/net.c",
65    "lib/portability.c",
66    "lib/tty.c",
67    "lib/xwrap.c",
68    "main.c",
69    "toys/lsb/gzip.c",
70    "toys/lsb/hostname.c",
71    "toys/lsb/md5sum.c",
72    "toys/lsb/mktemp.c",
73    "toys/lsb/seq.c",
74    "toys/net/microcom.c",
75    "toys/other/dos2unix.c",
76    "toys/other/readlink.c",
77    "toys/other/setsid.c",
78    "toys/other/stat.c",
79    "toys/other/timeout.c",
80    "toys/other/truncate.c",
81    "toys/other/which.c",
82    "toys/other/xxd.c",
83    "toys/other/yes.c",
84    "toys/pending/dd.c",
85    "toys/pending/diff.c",
86    "toys/pending/expr.c",
87    "toys/pending/getopt.c",
88    "toys/pending/tr.c",
89    "toys/posix/basename.c",
90    "toys/posix/cat.c",
91    "toys/posix/chmod.c",
92    "toys/posix/cmp.c",
93    "toys/posix/comm.c",
94    "toys/posix/cp.c",
95    "toys/posix/cpio.c",
96    "toys/posix/cut.c",
97    "toys/posix/date.c",
98    "toys/posix/dirname.c",
99    "toys/posix/du.c",
100    "toys/posix/echo.c",
101    "toys/posix/env.c",
102    "toys/posix/find.c",
103    "toys/posix/getconf.c",
104    "toys/posix/grep.c",
105    "toys/posix/head.c",
106    "toys/posix/id.c",
107    "toys/posix/ln.c",
108    "toys/posix/ls.c",
109    "toys/posix/mkdir.c",
110    "toys/posix/nl.c",
111    "toys/posix/od.c",
112    "toys/posix/paste.c",
113    "toys/posix/patch.c",
114    "toys/posix/printf.c",
115    "toys/posix/pwd.c",
116    "toys/posix/rm.c",
117    "toys/posix/rmdir.c",
118    "toys/posix/sed.c",
119    "toys/posix/sleep.c",
120    "toys/posix/sort.c",
121    "toys/posix/tail.c",
122    "toys/posix/tar.c",
123    "toys/posix/tee.c",
124    "toys/posix/test.c",
125    "toys/posix/touch.c",
126    "toys/posix/true.c",
127    "toys/posix/uname.c",
128    "toys/posix/uniq.c",
129    "toys/posix/wc.c",
130    "toys/posix/xargs.c",
131]
132
133linux_srcs = [
134    "toys/posix/ps.c",
135    "toys/other/taskset.c",
136]
137
138device_srcs = [
139    "toys/android/getenforce.c",
140    "toys/android/load_policy.c",
141    "toys/android/log.c",
142    "toys/android/restorecon.c",
143    "toys/android/runcon.c",
144    "toys/android/sendevent.c",
145    "toys/android/setenforce.c",
146    "toys/lsb/dmesg.c",
147    "toys/lsb/killall.c",
148    "toys/lsb/mknod.c",
149    "toys/lsb/mount.c",
150    "toys/lsb/pidof.c",
151    "toys/lsb/sync.c",
152    "toys/lsb/umount.c",
153    "toys/net/ifconfig.c",
154    "toys/net/netcat.c",
155    "toys/net/netstat.c",
156    "toys/net/ping.c",
157    "toys/net/rfkill.c",
158    "toys/net/tunctl.c",
159    "toys/other/acpi.c",
160    "toys/other/base64.c",
161    "toys/other/blkdiscard.c",
162    "toys/other/blkid.c",
163    "toys/other/blockdev.c",
164    "toys/other/chcon.c",
165    "toys/other/chroot.c",
166    "toys/other/chrt.c",
167    "toys/other/clear.c",
168    "toys/other/devmem.c",
169    "toys/other/fallocate.c",
170    "toys/other/flock.c",
171    "toys/other/fmt.c",
172    "toys/other/free.c",
173    "toys/other/freeramdisk.c",
174    "toys/other/fsfreeze.c",
175    "toys/other/fsync.c",
176    "toys/other/help.c",
177    "toys/other/hwclock.c",
178    "toys/other/i2ctools.c",
179    "toys/other/inotifyd.c",
180    "toys/other/insmod.c",
181    "toys/other/ionice.c",
182    "toys/other/losetup.c",
183    "toys/other/lsattr.c",
184    "toys/other/lsmod.c",
185    "toys/other/lspci.c",
186    "toys/other/lsusb.c",
187    "toys/other/makedevs.c",
188    "toys/other/mkswap.c",
189    "toys/other/modinfo.c",
190    "toys/other/mountpoint.c",
191    "toys/other/nbd_client.c",
192    "toys/other/nsenter.c",
193    "toys/other/partprobe.c",
194    "toys/other/pivot_root.c",
195    "toys/other/pmap.c",
196    "toys/other/printenv.c",
197    "toys/other/pwdx.c",
198    "toys/other/rev.c",
199    "toys/other/rmmod.c",
200    "toys/other/rtcwake.c",
201    "toys/other/setfattr.c",
202    "toys/other/swapoff.c",
203    "toys/other/swapon.c",
204    "toys/other/sysctl.c",
205    "toys/other/tac.c",
206    "toys/other/uptime.c",
207    "toys/other/usleep.c",
208    "toys/other/uuidgen.c",
209    "toys/other/vconfig.c",
210    "toys/other/vmstat.c",
211    "toys/other/watch.c",
212    "toys/pending/getfattr.c",
213    "toys/pending/lsof.c",
214    "toys/pending/modprobe.c",
215    "toys/pending/more.c",
216    "toys/pending/readelf.c",
217    "toys/pending/stty.c",
218    "toys/pending/traceroute.c",
219    "toys/pending/vi.c",
220    "toys/posix/cal.c",
221    "toys/posix/chgrp.c",
222    "toys/posix/cksum.c",
223    "toys/posix/df.c",
224    "toys/posix/expand.c",
225    "toys/posix/false.c",
226    "toys/posix/file.c",
227    "toys/posix/iconv.c",
228    "toys/posix/kill.c",
229    "toys/posix/mkfifo.c",
230    "toys/posix/nice.c",
231    "toys/posix/nohup.c",
232    "toys/posix/renice.c",
233    "toys/posix/split.c",
234    "toys/posix/strings.c",
235    "toys/posix/time.c",
236    "toys/posix/tty.c",
237    "toys/posix/ulimit.c",
238    "toys/posix/unlink.c",
239    "toys/posix/uudecode.c",
240    "toys/posix/uuencode.c",
241]
242
243toybox_symlinks = [
244    "[",
245    "acpi",
246    "base64",
247    "basename",
248    "blockdev",
249    "cal",
250    "cat",
251    "chattr",
252    "chcon",
253    "chgrp",
254    "chmod",
255    "chown",
256    "chroot",
257    "chrt",
258    "cksum",
259    "clear",
260    "comm",
261    "cmp",
262    "cp",
263    "cpio",
264    "cut",
265    "date",
266    "dd",
267    "devmem",
268    "df",
269    "diff",
270    "dirname",
271    "dmesg",
272    "dos2unix",
273    "du",
274    "echo",
275    "egrep",
276    "env",
277    "expand",
278    "expr",
279    "fallocate",
280    "false",
281    "fgrep",
282    "file",
283    "find",
284    "flock",
285    "fmt",
286    "free",
287    "fsync",
288    "getconf",
289    "getenforce",
290    "grep",
291    "groups",
292    "gunzip",
293    "gzip",
294    "head",
295    "hostname",
296    "hwclock",
297    "i2cdetect",
298    "i2cdump",
299    "i2cget",
300    "i2cset",
301    "iconv",
302    "id",
303    "ifconfig",
304    "inotifyd",
305    "insmod",
306    "install",
307    "ionice",
308    "iorenice",
309    "kill",
310    "killall",
311    "load_policy",
312    "ln",
313    "log",
314    "logname",
315    "losetup",
316    "ls",
317    "lsattr",
318    "lsmod",
319    "lsof",
320    "lspci",
321    "lsusb",
322    "md5sum",
323    "mkdir",
324    "mkfifo",
325    "mknod",
326    "mkswap",
327    "mktemp",
328    "microcom",
329    "modinfo",
330    "more",
331    "mount",
332    "mountpoint",
333    "mv",
334    "nc",
335    "netcat",
336    "netstat",
337    "nice",
338    "nl",
339    "nohup",
340    "nproc",
341    "nsenter",
342    "od",
343    "paste",
344    "patch",
345    "pgrep",
346    "pidof",
347    "pkill",
348    "pmap",
349    "printenv",
350    "printf",
351    "ps",
352    "pwd",
353    "readelf",
354    "readlink",
355    "realpath",
356    "renice",
357    "restorecon",
358    "rm",
359    "rmdir",
360    "rmmod",
361    "rtcwake",
362    "runcon",
363    "sed",
364    "sendevent",
365    "seq",
366    "setenforce",
367    "setsid",
368    "sha1sum",
369    "sha224sum",
370    "sha256sum",
371    "sha384sum",
372    "sha512sum",
373    "sleep",
374    "sort",
375    "split",
376    "stat",
377    "strings",
378    "stty",
379    "swapoff",
380    "swapon",
381    "sync",
382    "sysctl",
383    "tac",
384    "tail",
385    "tar",
386    "taskset",
387    "tee",
388    "test",
389    "time",
390    "timeout",
391    "top",
392    "touch",
393    "tr",
394    "true",
395    "truncate",
396    "tty",
397    "ulimit",
398    "umount",
399    "uname",
400    "uniq",
401    "unix2dos",
402    "unlink",
403    "unshare",
404    "uptime",
405    "usleep",
406    "uudecode",
407    "uuencode",
408    "uuidgen",
409    "vmstat",
410    "watch",
411    "wc",
412    "which",
413    "whoami",
414    "xargs",
415    "xxd",
416    "yes",
417    "zcat",
418]
419
420cc_defaults {
421    name: "toybox-defaults",
422    srcs: all_srcs,
423
424    cflags: [
425        "-Os",
426        "-Wall",
427        "-Werror",
428        "-Wno-char-subscripts",
429        "-Wno-deprecated-declarations",
430        "-Wno-missing-field-initializers",
431        "-Wno-pointer-arith",
432        "-Wno-sign-compare",
433        "-Wno-string-plus-int",
434        "-Wno-unused-parameter",
435        "-Wno-unused-variable",
436        "-funsigned-char",
437        "-ffunction-sections",
438        "-fdata-sections",
439        "-fno-asynchronous-unwind-tables",
440        "-DTOYBOX_VENDOR=\"-android\"",
441    ],
442
443    target: {
444        linux_glibc: {
445            local_include_dirs: ["android/linux"],
446        },
447
448        linux_bionic: {
449            local_include_dirs: ["android/linux"],
450        },
451
452        darwin: {
453            local_include_dirs: ["android/mac"],
454            cflags: [
455                // You can't have toybox cp(1) on macOS before 10.13.
456                "-mmacosx-version-min=10.13",
457                "-UMACOSX_DEPLOYMENT_TARGET",
458                "-DMACOSX_DEPLOYMENT_TARGET=10.13",
459                // macOS' getgroups(3) signature differs.
460                "-Wno-pointer-sign",
461                // diff.c defines MIN and MAX which (only on macOS) we're
462                // also getting from <sys/param.h>.
463                "-Wno-macro-redefined",
464            ],
465        },
466
467        linux: {
468            srcs: linux_srcs,
469        },
470
471        android: {
472            local_include_dirs: ["android/device"],
473            srcs: device_srcs,
474        },
475    },
476}
477
478//###########################################
479// toybox for /system, /vendor, and /recovery
480//###########################################
481
482cc_defaults {
483    name: "toybox-shared-defaults",
484    defaults: ["toybox-defaults"],
485
486    // This doesn't actually prevent us from dragging in libc++ at runtime
487    // on the device because libnetd_client.so is C++, but it improves host
488    // startup time.
489    stl: "none",
490
491    shared_libs: [
492        "libcrypto",
493        "libz",
494    ],
495
496    target: {
497        android: {
498            shared_libs: [
499                "liblog",
500                "libprocessgroup",
501                "libselinux",
502            ],
503            symlinks: toybox_symlinks,
504        },
505    },
506}
507
508cc_binary {
509    name: "toybox",
510    defaults: ["toybox-shared-defaults"],
511    host_supported: true,
512    recovery_available: true,
513    vendor_ramdisk_available: true,
514}
515
516cc_binary {
517    name: "toybox_vendor",
518    defaults: ["toybox-shared-defaults"],
519    vendor: true,
520}
521
522//###########################################
523// Static toybox binaries for legacy devices
524//###########################################
525
526cc_binary {
527    name: "toybox-static",
528    defaults: ["toybox-defaults"],
529    static_executable: true,
530    compile_multilib: "both",
531    multilib: {
532        lib32: { suffix: "32", },
533        lib64: { suffix: "64", },
534    },
535    stl: "libc++_static",
536    static_libs: [
537        "libc",
538        "libm",
539        "libz",
540        "libbase",
541        "libcgrouprc",
542        "libcgrouprc_format",
543        "libcrypto_static",
544        "liblog",
545        "libprocessgroup",
546        "libselinux",
547    ],
548    dist: {
549        targets: [
550            "sdk",
551        ],
552    },
553}
554
555//###########################################
556// Running the toybox tests
557//###########################################
558
559sh_test {
560    name: "toybox-tests",
561    src: "run-tests-on-android.sh",
562    filename: "run-tests-on-android.sh",
563    test_suites: ["general-tests"],
564    host_supported: true,
565    device_supported: false,
566    require_root:true,
567    data: [
568        "tests/**/*",
569        "scripts/runtest.sh",
570    ],
571}
572