github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/builder/picolibc.go (about)

     1  package builder
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	"github.com/tinygo-org/tinygo/goenv"
     9  )
    10  
    11  // Picolibc is a C library for bare metal embedded devices. It was originally
    12  // based on newlib.
    13  var Picolibc = Library{
    14  	name: "picolibc",
    15  	makeHeaders: func(target, includeDir string) error {
    16  		f, err := os.Create(filepath.Join(includeDir, "picolibc.h"))
    17  		if err != nil {
    18  			return err
    19  		}
    20  		return f.Close()
    21  	},
    22  	cflags: func(target, headerPath string) []string {
    23  		newlibDir := filepath.Join(goenv.Get("TINYGOROOT"), "lib/picolibc/newlib")
    24  		return []string{
    25  			"-Werror",
    26  			"-Wall",
    27  			"-std=gnu11",
    28  			"-D_COMPILING_NEWLIB",
    29  			"-D_HAVE_ALIAS_ATTRIBUTE",
    30  			"-DTINY_STDIO",
    31  			"-DPOSIX_IO",
    32  			"-D_IEEE_LIBM",
    33  			"-D__OBSOLETE_MATH_FLOAT=1", // use old math code that doesn't expect a FPU
    34  			"-D__OBSOLETE_MATH_DOUBLE=0",
    35  			"-D_WANT_IO_C99_FORMATS",
    36  			"-nostdlibinc",
    37  			"-isystem", newlibDir + "/libc/include",
    38  			"-I" + newlibDir + "/libc/tinystdio",
    39  			"-I" + newlibDir + "/libm/common",
    40  			"-I" + headerPath,
    41  		}
    42  	},
    43  	sourceDir: func() string { return filepath.Join(goenv.Get("TINYGOROOT"), "lib/picolibc/newlib") },
    44  	librarySources: func(target string) ([]string, error) {
    45  		sources := append([]string(nil), picolibcSources...)
    46  		if !strings.HasPrefix(target, "avr") {
    47  			// Small chips without long jumps can't compile many files (printf,
    48  			// pow, etc). Therefore exclude those source files for those chips.
    49  			// Unfortunately it's difficult to exclude only some chips, so this
    50  			// excludes those files on all AVR chips for now.
    51  			// More information:
    52  			// https://github.com/llvm/llvm-project/issues/67042
    53  			sources = append(sources, picolibcSourcesLarge...)
    54  		}
    55  		return sources, nil
    56  	},
    57  }
    58  
    59  var picolibcSources = []string{
    60  	"../../picolibc-stdio.c",
    61  
    62  	"libc/string/bcmp.c",
    63  	"libc/string/bcopy.c",
    64  	"libc/string/bzero.c",
    65  	"libc/string/explicit_bzero.c",
    66  	"libc/string/ffsl.c",
    67  	"libc/string/ffsll.c",
    68  	"libc/string/fls.c",
    69  	"libc/string/flsl.c",
    70  	"libc/string/flsll.c",
    71  	"libc/string/gnu_basename.c",
    72  	"libc/string/index.c",
    73  	"libc/string/memccpy.c",
    74  	"libc/string/memchr.c",
    75  	"libc/string/memcmp.c",
    76  	"libc/string/memcpy.c",
    77  	"libc/string/memmem.c",
    78  	"libc/string/memmove.c",
    79  	"libc/string/mempcpy.c",
    80  	"libc/string/memrchr.c",
    81  	"libc/string/memset.c",
    82  	"libc/string/rawmemchr.c",
    83  	"libc/string/rindex.c",
    84  	"libc/string/stpcpy.c",
    85  	"libc/string/stpncpy.c",
    86  	"libc/string/strcasecmp.c",
    87  	"libc/string/strcasecmp_l.c",
    88  	"libc/string/strcasestr.c",
    89  	"libc/string/strcat.c",
    90  	"libc/string/strchr.c",
    91  	"libc/string/strchrnul.c",
    92  	"libc/string/strcmp.c",
    93  	"libc/string/strcoll.c",
    94  	"libc/string/strcoll_l.c",
    95  	"libc/string/strcpy.c",
    96  	"libc/string/strcspn.c",
    97  	"libc/string/strdup.c",
    98  	"libc/string/strerror.c",
    99  	"libc/string/strerror_r.c",
   100  	"libc/string/strlcat.c",
   101  	"libc/string/strlcpy.c",
   102  	"libc/string/strlen.c",
   103  	"libc/string/strlwr.c",
   104  	"libc/string/strncasecmp.c",
   105  	"libc/string/strncasecmp_l.c",
   106  	"libc/string/strncat.c",
   107  	"libc/string/strncmp.c",
   108  	"libc/string/strncpy.c",
   109  	"libc/string/strndup.c",
   110  	"libc/string/strnlen.c",
   111  	"libc/string/strnstr.c",
   112  	"libc/string/strpbrk.c",
   113  	"libc/string/strrchr.c",
   114  	"libc/string/strsep.c",
   115  	"libc/string/strsignal.c",
   116  	"libc/string/strspn.c",
   117  	"libc/string/strstr.c",
   118  	"libc/string/strtok.c",
   119  	"libc/string/strtok_r.c",
   120  	"libc/string/strupr.c",
   121  	"libc/string/strverscmp.c",
   122  	"libc/string/strxfrm.c",
   123  	"libc/string/strxfrm_l.c",
   124  	"libc/string/swab.c",
   125  	"libc/string/timingsafe_bcmp.c",
   126  	"libc/string/timingsafe_memcmp.c",
   127  	"libc/string/u_strerr.c",
   128  	"libc/string/wcpcpy.c",
   129  	"libc/string/wcpncpy.c",
   130  	"libc/string/wcscasecmp.c",
   131  	"libc/string/wcscasecmp_l.c",
   132  	"libc/string/wcscat.c",
   133  	"libc/string/wcschr.c",
   134  	"libc/string/wcscmp.c",
   135  	"libc/string/wcscoll.c",
   136  	"libc/string/wcscoll_l.c",
   137  	"libc/string/wcscpy.c",
   138  	"libc/string/wcscspn.c",
   139  	"libc/string/wcsdup.c",
   140  	"libc/string/wcslcat.c",
   141  	"libc/string/wcslcpy.c",
   142  	"libc/string/wcslen.c",
   143  	"libc/string/wcsncasecmp.c",
   144  	"libc/string/wcsncasecmp_l.c",
   145  	"libc/string/wcsncat.c",
   146  	"libc/string/wcsncmp.c",
   147  	"libc/string/wcsncpy.c",
   148  	"libc/string/wcsnlen.c",
   149  	"libc/string/wcspbrk.c",
   150  	"libc/string/wcsrchr.c",
   151  	"libc/string/wcsspn.c",
   152  	"libc/string/wcsstr.c",
   153  	"libc/string/wcstok.c",
   154  	"libc/string/wcswidth.c",
   155  	"libc/string/wcsxfrm.c",
   156  	"libc/string/wcsxfrm_l.c",
   157  	"libc/string/wcwidth.c",
   158  	"libc/string/wmemchr.c",
   159  	"libc/string/wmemcmp.c",
   160  	"libc/string/wmemcpy.c",
   161  	"libc/string/wmemmove.c",
   162  	"libc/string/wmempcpy.c",
   163  	"libc/string/wmemset.c",
   164  	"libc/string/xpg_strerror_r.c",
   165  }
   166  
   167  // Parts of picolibc that are too large for small AVRs.
   168  var picolibcSourcesLarge = []string{
   169  	// srcs_tinystdio
   170  	"libc/tinystdio/asprintf.c",
   171  	"libc/tinystdio/bufio.c",
   172  	"libc/tinystdio/clearerr.c",
   173  	"libc/tinystdio/ecvt_r.c",
   174  	"libc/tinystdio/ecvt.c",
   175  	"libc/tinystdio/ecvtf_r.c",
   176  	"libc/tinystdio/ecvtf.c",
   177  	"libc/tinystdio/fcvt.c",
   178  	"libc/tinystdio/fcvt_r.c",
   179  	"libc/tinystdio/fcvtf.c",
   180  	"libc/tinystdio/fcvtf_r.c",
   181  	"libc/tinystdio/gcvt.c",
   182  	"libc/tinystdio/gcvtf.c",
   183  	"libc/tinystdio/fclose.c",
   184  	"libc/tinystdio/fdevopen.c",
   185  	"libc/tinystdio/feof.c",
   186  	"libc/tinystdio/ferror.c",
   187  	"libc/tinystdio/fflush.c",
   188  	"libc/tinystdio/fgetc.c",
   189  	"libc/tinystdio/fgets.c",
   190  	"libc/tinystdio/fileno.c",
   191  	"libc/tinystdio/filestrget.c",
   192  	"libc/tinystdio/filestrput.c",
   193  	"libc/tinystdio/filestrputalloc.c",
   194  	"libc/tinystdio/fmemopen.c",
   195  	"libc/tinystdio/fprintf.c",
   196  	"libc/tinystdio/fputc.c",
   197  	"libc/tinystdio/fputs.c",
   198  	"libc/tinystdio/fread.c",
   199  	//"libc/tinystdio/freopen.c", // crashes with AVR, see: https://github.com/picolibc/picolibc/pull/369
   200  	"libc/tinystdio/fscanf.c",
   201  	"libc/tinystdio/fseek.c",
   202  	"libc/tinystdio/fseeko.c",
   203  	"libc/tinystdio/ftell.c",
   204  	"libc/tinystdio/ftello.c",
   205  	"libc/tinystdio/fwrite.c",
   206  	"libc/tinystdio/getchar.c",
   207  	"libc/tinystdio/gets.c",
   208  	"libc/tinystdio/matchcaseprefix.c",
   209  	"libc/tinystdio/mktemp.c",
   210  	"libc/tinystdio/perror.c",
   211  	"libc/tinystdio/printf.c",
   212  	"libc/tinystdio/putchar.c",
   213  	"libc/tinystdio/puts.c",
   214  	"libc/tinystdio/rewind.c",
   215  	"libc/tinystdio/scanf.c",
   216  	"libc/tinystdio/setbuf.c",
   217  	"libc/tinystdio/setbuffer.c",
   218  	"libc/tinystdio/setlinebuf.c",
   219  	"libc/tinystdio/setvbuf.c",
   220  	"libc/tinystdio/snprintf.c",
   221  	"libc/tinystdio/sprintf.c",
   222  	"libc/tinystdio/snprintfd.c",
   223  	"libc/tinystdio/snprintff.c",
   224  	"libc/tinystdio/sprintff.c",
   225  	"libc/tinystdio/sprintfd.c",
   226  	"libc/tinystdio/sscanf.c",
   227  	"libc/tinystdio/strfromf.c",
   228  	"libc/tinystdio/strfromd.c",
   229  	"libc/tinystdio/strtof.c",
   230  	"libc/tinystdio/strtof_l.c",
   231  	"libc/tinystdio/strtod.c",
   232  	"libc/tinystdio/strtod_l.c",
   233  	"libc/tinystdio/ungetc.c",
   234  	"libc/tinystdio/vasprintf.c",
   235  	"libc/tinystdio/vfiprintf.c",
   236  	"libc/tinystdio/vfprintf.c",
   237  	"libc/tinystdio/vfprintff.c",
   238  	"libc/tinystdio/vfscanf.c",
   239  	"libc/tinystdio/vfiscanf.c",
   240  	"libc/tinystdio/vfscanff.c",
   241  	"libc/tinystdio/vprintf.c",
   242  	"libc/tinystdio/vscanf.c",
   243  	"libc/tinystdio/vsscanf.c",
   244  	"libc/tinystdio/vsnprintf.c",
   245  	"libc/tinystdio/vsprintf.c",
   246  
   247  	"libm/common/sf_finite.c",
   248  	"libm/common/sf_copysign.c",
   249  	"libm/common/sf_modf.c",
   250  	"libm/common/sf_scalbn.c",
   251  	"libm/common/sf_cbrt.c",
   252  	"libm/common/sf_exp10.c",
   253  	"libm/common/sf_expm1.c",
   254  	"libm/common/sf_ilogb.c",
   255  	"libm/common/sf_infinity.c",
   256  	"libm/common/sf_isinf.c",
   257  	"libm/common/sf_isinff.c",
   258  	"libm/common/sf_isnan.c",
   259  	"libm/common/sf_isnanf.c",
   260  	"libm/common/sf_issignaling.c",
   261  	"libm/common/sf_log1p.c",
   262  	"libm/common/sf_nan.c",
   263  	"libm/common/sf_nextafter.c",
   264  	"libm/common/sf_pow10.c",
   265  	"libm/common/sf_rint.c",
   266  	"libm/common/sf_logb.c",
   267  	"libm/common/sf_fdim.c",
   268  	"libm/common/sf_fma.c",
   269  	"libm/common/sf_fmax.c",
   270  	"libm/common/sf_fmin.c",
   271  	"libm/common/sf_fpclassify.c",
   272  	"libm/common/sf_lrint.c",
   273  	"libm/common/sf_llrint.c",
   274  	"libm/common/sf_lround.c",
   275  	"libm/common/sf_llround.c",
   276  	"libm/common/sf_nearbyint.c",
   277  	"libm/common/sf_remquo.c",
   278  	"libm/common/sf_round.c",
   279  	"libm/common/sf_scalbln.c",
   280  	"libm/common/sf_trunc.c",
   281  	"libm/common/sf_exp.c",
   282  	"libm/common/sf_exp2.c",
   283  	"libm/common/sf_exp2_data.c",
   284  	"libm/common/sf_log.c",
   285  	"libm/common/sf_log_data.c",
   286  	"libm/common/sf_log2.c",
   287  	"libm/common/sf_log2_data.c",
   288  	"libm/common/sf_pow_log2_data.c",
   289  	"libm/common/sf_pow.c",
   290  
   291  	"libm/common/s_finite.c",
   292  	"libm/common/s_copysign.c",
   293  	"libm/common/s_modf.c",
   294  	"libm/common/s_scalbn.c",
   295  	"libm/common/s_cbrt.c",
   296  	"libm/common/s_exp10.c",
   297  	"libm/common/s_expm1.c",
   298  	"libm/common/s_ilogb.c",
   299  	"libm/common/s_infinity.c",
   300  	"libm/common/s_iseqsig.c",
   301  	"libm/common/s_isinf.c",
   302  	"libm/common/s_isinfd.c",
   303  	"libm/common/s_isnan.c",
   304  	"libm/common/s_isnand.c",
   305  	"libm/common/s_issignaling.c",
   306  	"libm/common/s_log1p.c",
   307  	"libm/common/s_nan.c",
   308  	"libm/common/s_nextafter.c",
   309  	"libm/common/s_pow10.c",
   310  	"libm/common/s_rint.c",
   311  	"libm/common/s_logb.c",
   312  	"libm/common/s_log2.c",
   313  	"libm/common/s_fdim.c",
   314  	"libm/common/s_fma.c",
   315  	"libm/common/s_fmax.c",
   316  	"libm/common/s_fmin.c",
   317  	"libm/common/s_fpclassify.c",
   318  	"libm/common/s_getpayload.c",
   319  	"libm/common/s_lrint.c",
   320  	"libm/common/s_llrint.c",
   321  	"libm/common/s_lround.c",
   322  	"libm/common/s_llround.c",
   323  	"libm/common/s_nearbyint.c",
   324  	"libm/common/s_remquo.c",
   325  	"libm/common/s_round.c",
   326  	"libm/common/s_scalbln.c",
   327  	"libm/common/s_signbit.c",
   328  	"libm/common/s_trunc.c",
   329  	"libm/common/exp.c",
   330  	"libm/common/exp2.c",
   331  	"libm/common/exp_data.c",
   332  	"libm/common/math_err_with_errno.c",
   333  	"libm/common/math_err_uflow.c",
   334  	"libm/common/math_err_oflow.c",
   335  	"libm/common/math_err_divzero.c",
   336  	"libm/common/math_err_invalid.c",
   337  	"libm/common/math_err_may_uflow.c",
   338  	"libm/common/math_err_check_uflow.c",
   339  	"libm/common/math_err_check_oflow.c",
   340  	"libm/common/math_errf_divzerof.c",
   341  	"libm/common/math_errf_invalidf.c",
   342  	"libm/common/math_errf_may_uflowf.c",
   343  	"libm/common/math_errf_oflowf.c",
   344  	"libm/common/math_errf_uflowf.c",
   345  	"libm/common/math_errf_with_errnof.c",
   346  	"libm/common/math_inexact.c",
   347  	"libm/common/math_inexactf.c",
   348  	"libm/common/log.c",
   349  	"libm/common/log_data.c",
   350  	"libm/common/log2.c",
   351  	"libm/common/log2_data.c",
   352  	"libm/common/pow.c",
   353  	"libm/common/pow_log_data.c",
   354  
   355  	"libm/math/k_cos.c",
   356  	"libm/math/k_rem_pio2.c",
   357  	"libm/math/k_sin.c",
   358  	"libm/math/k_tan.c",
   359  	"libm/math/kf_cos.c",
   360  	"libm/math/kf_rem_pio2.c",
   361  	"libm/math/kf_sin.c",
   362  	"libm/math/kf_tan.c",
   363  	"libm/math/s_acos.c",
   364  	"libm/math/s_acosh.c",
   365  	"libm/math/s_asin.c",
   366  	"libm/math/s_asinh.c",
   367  	"libm/math/s_atan.c",
   368  	"libm/math/s_atan2.c",
   369  	"libm/math/s_atanh.c",
   370  	"libm/math/s_ceil.c",
   371  	"libm/math/s_cos.c",
   372  	"libm/math/s_cosh.c",
   373  	"libm/math/s_drem.c",
   374  	"libm/math/s_erf.c",
   375  	"libm/math/s_exp.c",
   376  	"libm/math/s_exp2.c",
   377  	"libm/math/s_fabs.c",
   378  	"libm/math/s_floor.c",
   379  	"libm/math/s_fmod.c",
   380  	"libm/math/s_frexp.c",
   381  	"libm/math/s_gamma.c",
   382  	"libm/math/s_hypot.c",
   383  	"libm/math/s_j0.c",
   384  	"libm/math/s_j1.c",
   385  	"libm/math/s_jn.c",
   386  	"libm/math/s_lgamma.c",
   387  	"libm/math/s_log.c",
   388  	"libm/math/s_log10.c",
   389  	"libm/math/s_pow.c",
   390  	"libm/math/s_rem_pio2.c",
   391  	"libm/math/s_remainder.c",
   392  	"libm/math/s_scalb.c",
   393  	"libm/math/s_signif.c",
   394  	"libm/math/s_sin.c",
   395  	"libm/math/s_sincos.c",
   396  	"libm/math/s_sinh.c",
   397  	"libm/math/s_sqrt.c",
   398  	"libm/math/s_tan.c",
   399  	"libm/math/s_tanh.c",
   400  	"libm/math/s_tgamma.c",
   401  	"libm/math/sf_acos.c",
   402  	"libm/math/sf_acosh.c",
   403  	"libm/math/sf_asin.c",
   404  	"libm/math/sf_asinh.c",
   405  	"libm/math/sf_atan.c",
   406  	"libm/math/sf_atan2.c",
   407  	"libm/math/sf_atanh.c",
   408  	"libm/math/sf_ceil.c",
   409  	"libm/math/sf_cos.c",
   410  	"libm/math/sf_cosh.c",
   411  	"libm/math/sf_drem.c",
   412  	"libm/math/sf_erf.c",
   413  	"libm/math/sf_exp.c",
   414  	"libm/math/sf_exp2.c",
   415  	"libm/math/sf_fabs.c",
   416  	"libm/math/sf_floor.c",
   417  	"libm/math/sf_fmod.c",
   418  	"libm/math/sf_frexp.c",
   419  	"libm/math/sf_gamma.c",
   420  	"libm/math/sf_hypot.c",
   421  	"libm/math/sf_j0.c",
   422  	"libm/math/sf_j1.c",
   423  	"libm/math/sf_jn.c",
   424  	"libm/math/sf_lgamma.c",
   425  	"libm/math/sf_log.c",
   426  	"libm/math/sf_log10.c",
   427  	"libm/math/sf_log2.c",
   428  	"libm/math/sf_pow.c",
   429  	"libm/math/sf_rem_pio2.c",
   430  	"libm/math/sf_remainder.c",
   431  	"libm/math/sf_scalb.c",
   432  	"libm/math/sf_signif.c",
   433  	"libm/math/sf_sin.c",
   434  	"libm/math/sf_sincos.c",
   435  	"libm/math/sf_sinh.c",
   436  	"libm/math/sf_sqrt.c",
   437  	"libm/math/sf_tan.c",
   438  	"libm/math/sf_tanh.c",
   439  	"libm/math/sf_tgamma.c",
   440  	"libm/math/sr_lgamma.c",
   441  	"libm/math/srf_lgamma.c",
   442  }