shithub: snippets

ref: f6dbc2ae6e153c5608b6f9d893238d71ae521935

View raw version
#!/bin/rc
# usage: rm -rf mkfile dir/plan9; helpmeport dir | rc
rfork ne

dir=$1
shift

if(! test -d $"dir){
	echo directory should be specified
	exit no
}

echo '# HEADERS'
echo mkdir -p $dir/plan9
g -h '#[ 	]*include[ 	]*<[^>/]+/' | sed 's☭.*<(.*)/[^/]*>.*☭mkdir -p '$dir'/plan9/\1☭g' $* | sort -u
g -h '#[ 	]*include[ 	]*<[^>/]+' | sed 's☭.*<(.*)>.*☭echo ''#include "plan9.h"'' > '$dir'/plan9/\1☭g' $* | sort -u
echo 'cat >'$dir'/plan9/plan9.h <<EOF
#ifndef __plan9_h__
#define __plan9_h__

#include <u.h>
#include <libc.h>
#include </sys/include/ctype.h>
#include </sys/include/stdio.h>

typedef enum { false, true } bool;

typedef s8int int8_t;
typedef u8int uint8_t;
typedef s16int int16_t;
typedef u16int uint16_t;
typedef s32int int32_t;
typedef u32int uint32_t;
typedef s64int int64_t;
typedef u64int uint64_t;
typedef long ssize_t;
typedef ulong size_t;
typedef intptr ptrdiff_t;
typedef intptr intptr_t;
typedef uintptr uintptr_t;

#define INT16_MAX 0x7fff
#define INT16_MIN ((int16_t)0x8000)
#define INT64_MIN ((int64_t)0x8000000000000000ULL)
#define INT32_MAX 0x7fffffff
#define INT_MAX INT32_MAX
#define INT32_MIN (-INT32_MAX-1)
#define INT_MIN INT32_MIN
#define SHRT_MAX 0x7fff
#define SHRT_MIN (-SHRT_MAX-1)
#define SIZE_MAX 0xffffffffU
#define UINT32_MAX 0xffffffffU
#define UINT_MAX UINT32_MAX
#define UINT64_MAX 0xffffffffffffffffULL

#define PRId64 "lld"
#define PRIu64 "llud"
#define PRIx64 "llx"

#define PATH_MAX 256

#define __attribute__(a)

#define exit(x) exits(x == 0 ? nil : "error")
#define setenv(k,v,x) putenv(k,v)
#define unsetenv(k) putenv(k,"")
#define strcasecmp cistrcmp
#define strncasecmp cistrncmp
#define getcwd getwd
#define sinf sin
#define cosf cos
#define usleep(x) sleep(x/1000)

#endif
EOF'

fn c2o {
	echo 'HEADERS=\'
	walk -f $* | grep '\.h$' | sed 's☭.*☭	&\\☭g'
	echo
	echo 'OFILES=\'
	walk -f $* | grep '\.c$' | sed 's☭(.*)\.c☭	\1.$O\\☭g'
	echo
#	walk -f $* | grep '\.c$' | sed 's☭(.*/)*([^/]+)\.c$☭	\2.$O\\☭g'
#	echo
#	walk -f $* | grep '/.*\.c$' | sed 's☭/[^/]+$☭☭g' | sort -u | sed 's☭.*☭%.$O: &/$stem.c\n	$CC $CFLAGS &/$stem.c\n☭g'
}

echo
echo '# MKFILE'
echo 'cat >mkfile <<''EOF'''
echo '</$objtype/mkfile'
echo
echo '#LIB=libFIXME.a$O'
echo '#BIN=/$objtype/bin'
echo '#TARG=FIXME'
echo 'CFLAGS=$CFLAGS -p -I'$dir' -I'$dir'/plan9 -DNDEBUG -D__plan9__ -D__${objtype}__'
echo
c2o $dir
echo 'default:V: all'
echo
echo 'CLEANFILES=$OFILES'
echo
echo '#FIXME'
echo '#</sys/src/cmd/mklib'
echo '#</sys/src/cmd/mkone'
echo
echo '%.$O: %.c'
echo '	$CC $CFLAGS -o $target $stem.c'
echo EOF