Skip to content

Instantly share code, notes, and snippets.

@ashafq
Created April 29, 2016 16:16
Show Gist options
  • Save ashafq/fe44921f19035bb369e5c0a2387f4f69 to your computer and use it in GitHub Desktop.
Save ashafq/fe44921f19035bb369e5c0a2387f4f69 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# This program generates a header file that is needed to figure out number of
# bits in different integer types.
#
header_file=$1
binary=`mktemp`
header_base=`basename "${header_file}"`
compile_guard=`echo "${header_base}" | tr '[:lower:]' '[:upper:]' | tr . _`
cat << EOF | gcc -o $binary -x c -
#include <stdio.h>
#include <limits.h>
#define PRINT_DEF(name, val) printf("#define %s (%u)\n", #name, val);
int main()
{
printf("#ifndef %s\n#define %s\n\n",
"${compile_guard}", "${compile_guard}");
PRINT_DEF(__CHAR_BITS__, sizeof(char) * CHAR_BIT);
PRINT_DEF(__SHORT_BITS__, sizeof(short) * CHAR_BIT);
PRINT_DEF(__INT_BITS__, sizeof(int) * CHAR_BIT);
PRINT_DEF(__LONG_BITS__, sizeof(long) * CHAR_BIT);
PRINT_DEF(__FLOAT_BITS__, sizeof(float) * CHAR_BIT);
PRINT_DEF(__DOUBLE_BITS__, sizeof(double) * CHAR_BIT);
PRINT_DEF(__LONG_DOUBLE_BITS__, sizeof(long double) * CHAR_BIT);
printf("\n#endif /* %s */\n", "${compile_guard}");
return 0;
}
EOF
$binary > $header_file
rm -f $binary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment