libk  Diff

Differences From Artifact [75da38cbfd]:

To Artifact [e19ff3649f]:


     1      1   #!/usr/bin/env bash
            2  +export out=${out:-out}
     2      3   . global/common.sh
     3      4   noimpl() {
     4      5   	say "$1 is not currently implemented on your system. an exciting chance for you to contribute, no?"
     5      6   	exit 1
     6      7   }
            8  +timestamp() { date "+%H:%M:%S %A"; }
     7      9   
     8     10   if test "$os$arch$bits" = ""; then
     9     11   	say "set the following environment variables to the appropriate atoms describing your system. for help determining the appropriate atoms, see libk.md"
    10     12   	say ' - $os={lin|fbsd|hai|osx…}'
    11     13   	say ' - $arch={x86|arm|ia64|mips…}'
    12     14   	say ' - $bits={|32|64…}'
    13     15   	exit 1
    14     16   fi
           17  +
           18  +if test "$1" = "-C"; then
           19  +	say "precleaning repo"
           20  +	./clean.sh
           21  +fi
    15     22   
    16     23   # TODO: make it possible for user to change
    17     24   #       default set with environment vars
    18     25   modules=(kcore kmem kstr kio kgraft kfile)
    19     26   
    20     27   target=$arch.$os
    21     28   if test "$bits" != ""; then
................................................................................
    31     38   case $os.$bits in
    32     39   	win.32) bin_fmt=win32;;
    33     40   	win.64) bin_fmt=win64;;
    34     41   	osx.32|dar.32) bin_fmt=macho32;;
    35     42   	osx.64|dar.64) bin_fmt=macho64;;
    36     43   	dos.*) bin_fmt=dosexe;;
    37     44   	none.*) bin_fmt=bin;;
    38         -	32) bin_fmt=elf32;;
    39         -	64) bin_fmt=elf64;;
           45  +	*.32) bin_fmt=elf32;;
           46  +	*.64) bin_fmt=elf64;;
           47  +	*) say "cannot determine correct binary format to use for target $target"; exit 1;;
    40     48   esac
    41     49   
    42     50   # first, we establish the
    43     51   # parameters of the build
    44         -has gcc && _cc=gcc
    45         -has clang && _cc=clang
    46     52   has cc && _cc=cc
           53  +has clang && _cc=clang 
           54  +has gcc && _cc=gcc
    47     55   cc=${cc:-$_cc}
    48     56   has m4 && _m4=m4
    49     57   m4=${m4:-$_m4}
    50         -has yasm && asm=yasm
    51     58   has nasm && asm=nasm
    52         -export out=${out:-out}
           59  +has yasm && asm=yasm
    53     60   export gen=${gen:-gen}
    54     61   library=${libkind:-static} # {static|shared|both}
    55     62   
    56     63   case $library in
    57     64   	static) build_static_library=yes
    58     65   	        build_shared_library=no;;
    59     66   
................................................................................
    85     92   
    86     93   macro_compile_env="-Datom_target_arch=$arch -Datom_target_os=$os -Dtarget_posix=$posix -Dtarget_unix=$unix"
    87     94   
    88     95   if test "$bits" != ""; then
    89     96   	macro_compile_env="$macro_compile_env -Datom_target_bits=$bits"
    90     97   fi
    91     98   
    92         -for mod in ${modules[@]}; do
    93         -	say "building libk with $mod"
    94         -done
    95     99   
    96    100   comp_mac() { $m4 $macro_compile_env -I "$gen" $3 "$1" > "$2"; }
    97         -comp_asm() { $asm "-f$bin_fmt" "-i$gen" $1 -o "$2"; }
    98         -comp_co()  { comp_clib $1 $2 -c; }
    99         -comp_clib(){
          101  +comp_asm() { $asm "-f$bin_fmt" -i "$gen" $1 -o "$2"; }
          102  +comp_co()  { comp_c $1 $2 "-c -fPIC"; }
          103  +comp_c(){
   100    104   	src=$1
   101    105   	output=$2
   102    106   	flags=$3
   103         -	$cc $src $3 -std=c11 -isystem "$out" -isystem "$gen" -isystem "arch/" -fPIC -nostdlib "-L$out" "-o$output"
          107  +	$cc $src $3 -std=c11 -isystem "$out" -isystem "$gen" -isystem "arch/" -nostdlib "-L$out" "-o$output"
   104    108   }
   105    109   
   106    110   scan() { find "$1" -name "$2"; }
   107    111   
   108         -# now we make sure all the appropriate
   109         -# directories exist
   110         -
          112  +say "commencing libk build $build at $(timestamp)"
   111    113   set -x
   112    114   
   113    115   # get type data
   114    116   mkdir -p $gen
   115    117   $cc -D_emit_m4_include arch/typesize.c -o $gen/typesize 
   116    118   $gen/typesize > gen/typesize.m
   117    119   
................................................................................
   132    134   
   133    135   # generate symbol tables for error handling functions
   134    136   mkdir -p $out/k
   135    137   awk -f global/gen-conds.awk <global/modules >$out/k/internal.egroup.h
   136    138   awk -f global/gen-ident.awk <global/modules >$gen/internal.ident.c
   137    139   comp_co $gen/internal.ident.c $out/internal.ident.o
   138    140   
   139         -# first pass: copy all raw headers into place
          141  +# first pass: copy all headers into place,
          142  +# including ones we need to generate
          143  +
   140    144   for mod in ${modules[@]}; do
   141    145   	for h in $(scan $mod '*.h'); do
   142    146   		base=$(basename $h)
   143    147   		cp "$h" "$out/k/$base"
   144    148   	done
   145         -done
   146    149   
   147         -# second pass: run macro headers through m4 and
   148         -# copy the resulting file into place
   149         -
   150         -for mod in ${modules[@]}; do
   151    150   	for h in $(scan $mod '*.h.m'); do
   152    151   		base=$(basename $h)
   153    152   		dest=${base%%.m}
   154    153   		comp_mac "$h" "$out/k/$dest"
   155    154   	done
   156    155   done
   157    156   
   158         -# third pass: generate manpage, html, and pdf
          157  +# second pass: generate manpage, html, and pdf
   159    158   # versions of the documentation from the md src
   160    159   
   161    160   mkdir -p $out/doc/{man,html,pdf}
   162    161   global/build-manpage.sh libk.md
   163    162   for mod in ${modules[@]}; do
   164    163   	for doc in $(scan $mod '*.md'); do
   165    164   		base="$(basename $doc)"
   166    165   		stem="${base%%.md}"
   167    166   		global/build-manpage.sh "$doc"
   168         -		# TODO html
   169         -		# TODO pdf
          167  +		global/build-html.sh "$doc"
          168  +		global/build-pdf.sh "$doc"
   170    169   	done
   171    170   done
   172    171   
   173         -# fourth pass: compile sources and save the
          172  +# third pass: compile sources and save the
   174    173   # resulting object files to $out, tracking
   175    174   # which is a runtime or function unit. exe's
   176    175   # will not be compiled until a later pass
   177    176   
   178    177   fn_objects=()
   179    178   rt_objects=()
   180    179   data_objects=( $out/internal.ident.o )
   181    180   for mod in ${modules[@]}; do
   182    181   	for fn in $(scan $mod '*.fn.c'); do
   183    182   		base="$(basename $fn)"
   184    183   		dest="$out/$mod.${base%%.c}.o"
   185    184   		comp_co "$fn" "$dest"
   186         -		fn_objects+="$dest"
          185  +		fn_objects+=("$dest")
   187    186   	done
   188    187   
   189    188   	for rt in $(scan $mod '*.rt.c'); do
   190    189   		base="$(basename $rt)"
   191    190   		dest="$out/$mod.${base%%.c}.o"
   192    191   		comp_co "$rt" "$dest"
   193         -		rt_objects+="$dest"
          192  +		rt_objects+=("$dest")
   194    193   	done
   195    194   
   196    195   	for fn in $(scan $mod "*.fn.$target.s"); do
   197    196   		base="$(basename $fn)"
   198    197   		dest="$out/$mod.${base%%.s}.o"
   199    198   		comp_asm "$fn" "$dest"
   200         -		fn_objects+="$dest"
          199  +		fn_objects+=("$dest")
   201    200   	done
   202    201   
   203    202   	for rt in $(scan $mod "*.rt.$target.s"); do
   204    203   		base="$(basename $rt)"
   205    204   		dest="$out/$mod.${base%%.s}.o"
   206    205   		comp_asm "$rt" "$dest"
   207         -		rt_objects+="$dest"
          206  +		rt_objects+=("$dest")
          207  +	done
          208  +done
          209  +
          210  +# fourth pass: link the libraries that are
          211  +# configured to be built
          212  +
          213  +if test $build_static_library == yes; then
          214  +	for obj in ${fn_objects[@]} ${rt_objects[@]} ${data_objects[@]}; do
          215  +		ar rc $out/libk.a $obj
          216  +	done
          217  +	ranlib $out/libk.a
          218  +fi
          219  +
          220  +if test $build_shared_library == yes; then
          221  +	ld -r ${rt_objects[@]} -o $out/boot.o
          222  +	ld -shared ${fn_objects[@]} -o $out/libk.so
          223  +fi
          224  +
          225  +# fifth pass: compile the executable tools
          226  +# against the libraries created in pass 5
          227  +
          228  +for mod in ${modules[@]}; do
          229  +	for exe in $(scan $mod '*.exe.c'); do
          230  +		base="$(basename $exe)"
          231  +		dest="$out/$mod.${base%%.exe.c}"
          232  +		if test $build_shared_library == yes; then
          233  +			comp_c "$out/boot.o $exe" "$dest" -lk
          234  +		else
          235  +			comp_c "$exe" "$dest" -lk
          236  +		fi
   208    237   	done
   209    238   done
   210    239   
   211         -echo fns: ${fn_objects[@]}
   212         -echo rts: ${rt_objects[@]}
          240  +set +x
          241  +say "all passes finished; build $build complete at $(timestamp)"