Skip to content
This repository has been archived by the owner on Jun 13, 2019. It is now read-only.

Commit

Permalink
Build: Generate enums and constants during build and add CI notification
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Schulhof committed Nov 10, 2015
1 parent b7c6d8f commit 8ae6a5a
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 602 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ sudo: false
language: cpp

matrix:
fast_finish: true
include:
- env: NODE_VERSION=0.10 TEST_SCRIPT=ci
os: linux
Expand Down Expand Up @@ -70,3 +71,8 @@ install:
- npm install -g grunt-cli

script: npm run-script "${TEST_SCRIPT}"

notifications:
irc:
channels:
- "chat.freenode.net#iotivity-node"
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ diff -u \
0. Modify the value of the variable ```version``` in ```octbstack.pc.in```.
0. Build and optionally install the new version of iotivity.
0. If you haven't installed iotivity in the previous step, you must now export environment variables ```OCTBSTACK_CFLAGS``` and ```OCTBSTACK_LIBS```, because the next step needs them.
0. Run ```./update-enums-and-constants.sh``` to update ```src/constants.cc``` and ```src/enums.cc``` followed by ```grunt format``` to re-format the source.
0. Run ```./update-enums.sh``` to update ```src/enums.cc``` followed by ```grunt format``` to re-format the source.
0. If the ```CFLAGS``` and/or ```LIBS``` have changed, modify ```install.sh``` and/or ```octbstack.pc.in``` and make the same modifications in ```binding.gyp```.
0. Test the build with both built-in iotivity and with iotivity via pkgconfig:
Expand All @@ -67,8 +67,8 @@ diff -u \
nvm use 0.10 && git clean -x -d -f -f && ./dist.sh --testonly
```

The build may fail in ```src/constants.cc``` or ```src/enums.cc```, complaining about undefined constants. That's because ```update-constants-and-enums.sh``` is unaware of C precompiler definitions and so it may harvest constants which are not actually defined under the set of precompiler flags used for building. Thus, you may need to edit ```src/constants.cc``` and ```src/enums.cc``` by hand **after** having run ```update-constants-and-enums.sh```.
The build may fail in ```src/constants.cc``` or ```src/enums.cc```, complaining about undefined constants. That's because ```update-enums.sh``` is unaware of C precompiler definitions and so it may harvest enum values which are not actually defined under the set of precompiler flags used for building. Thus, you may need to edit ```src/enums.cc``` by hand **after** having run ```update-enums.sh```.

The script ```./update-constants-and-enums.sh``` reads the C SDK header files and generates the contents of ```src/constants.cc``` and ```src/enums.cc```. Read the comments in the script before you modify either ```src/constants.cc``` or ```src/enums.cc```.
The script ```./update-enums.sh``` reads the C SDK header files and generates the contents of ```src/enums.cc```. Read the comments in the script before you modify ```src/enums.cc```.

[jQuery]: http://contribute.jquery.org/style-guide/js/
94 changes: 71 additions & 23 deletions binding.gyp
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
{
"variables": {
"externalOCTBStack": '<!(if test "x${OCTBSTACK_CFLAGS}x" != "xx" -a "x${OCTBSTACK_CFLAGS}x" != "xx"; then echo true; else echo false; fi)',

# Used when externalOCTBStack is false
"internal_octbstack_cflags": [
'<!@(echo "-I$(pwd)/deps/iotivity/include/iotivity/resource/csdk/stack/include")',
'<!@(echo "-I$(pwd)/deps/iotivity/include/iotivity/resource/csdk/ocrandom/include")',
'<!@(echo "-I$(pwd)/deps/iotivity/include/iotivity/resource/c_common")',
'-DROUTING_EP',
'-DTCP_ADAPTER'
]
},

"conditions": [
Expand All @@ -21,7 +30,8 @@

"target_defaults": {
"include_dirs": [
"<!(node -e \"require('nan')\")"
"<!(node -e \"require('nan')\")",
"<!@(echo \"$(pwd)/src\")"
],
"conditions": [

Expand All @@ -39,21 +49,9 @@
'-loctbstack',
'<!@(echo "-Wl,-rpath $(pwd)/deps/iotivity/lib")'
],
"cflags": [
'<!@(echo "-I$(pwd)/deps/iotivity/include/iotivity/resource/csdk/stack/include")',
'<!@(echo "-I$(pwd)/deps/iotivity/include/iotivity/resource/csdk/ocrandom/include")',
'<!@(echo "-I$(pwd)/deps/iotivity/include/iotivity/resource/c_common")',
'-DROUTING_EP',
'-DTCP_ADAPTER'
],
"cflags": [ '<(internal_octbstack_cflags)' ],
"xcode_settings": {
"OTHER_CFLAGS": [
'<!@(echo "-I$(pwd)/deps/iotivity/include/iotivity/resource/csdk/stack/include")',
'<!@(echo "-I$(pwd)/deps/iotivity/include/iotivity/resource/csdk/ocrandom/include")',
'<!@(echo "-I$(pwd)/deps/iotivity/include/iotivity/resource/c_common")',
'-DROUTING_EP',
'-DTCP_ADAPTER'
]
"OTHER_CFLAGS": [ '<(internal_octbstack_cflags)' ]
}
} ],

Expand All @@ -78,15 +76,13 @@
"targets": [
{
"target_name": "csdk",
"type": "none",
"conditions": [
[ "'<(externalOCTBStack)'=='false'", {
"actions": [ {
"action_name": "build",
"inputs": [],
"outputs": [
"deps/iotivity/lib/liboctbstack.so",
"deps/iotivity/include"
],
"inputs": [""],
"outputs": ["deps/iotivity"],
"action": [
"sh",
"./build-csdk.sh",
Expand All @@ -97,11 +93,63 @@
} ]
]
},
{
"target_name": "generateconstants",
"type": "none",
"actions": [ {
"action_name": "generateconstants",
"message": "Generating constants",
"inputs": ["deps/iotivity"],
"outputs": ["generated/constants.cc"],
"conditions": [
[ "'<(externalOCTBStack)'=='false'", {
"action": [
"sh",
"./generate-constants.sh",
'<(internal_octbstack_cflags)'
]
}, {
"action": [
"sh",
"./generate-constants.sh",
'<!@(echo "$OCTBSTACK_CFLAGS")'
]
} ]
]
} ],
"dependencies": [ "csdk" ]
},
{
"target_name": "generateenums",
"type": "none",
"actions": [ {
"action_name": "generateenums",
"message": "Generating enums",
"inputs": ["deps/iotivity"],
"outputs": ["generated/enums.cc"],
"conditions": [
[ "'<(externalOCTBStack)'=='false'", {
"action": [
"sh",
"./generate-enums.sh",
'<(internal_octbstack_cflags)'
]
}, {
"action": [
"sh",
"./generate-enums.sh",
'<!@(echo "$OCTBSTACK_CFLAGS")'
]
} ]
]
} ],
"dependencies": [ "csdk" ]
},
{
"target_name": "iotivity",
"sources": [
"src/constants.cc",
"src/enums.cc",
"generated/constants.cc",
"generated/enums.cc",
"src/functions.cc",
"src/functions/oc-cancel.cc",
"src/functions/oc-create-delete-resource.cc",
Expand All @@ -128,7 +176,7 @@
"defines": [ "TESTING" ]
} ]
],
"dependencies": [ "csdk" ]
"dependencies": [ "csdk", "generateconstants", "generateenums" ]
}
]
}
36 changes: 36 additions & 0 deletions constants-and-enums.common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Code that's common between looking for enums and looking for constants
# Assumes the command line consists of OCTBSTACK_CFLAGS

find_include_paths() {
echo "$OCTBSTACK_CFLAGS" | awk '{
for ( idx = 1 ; idx <= NF ; idx++ ) {
if ( substr( $idx, 1, 2 ) == "-I" ) {
if ( length( $idx ) == 2 ) {
print( $(idx + 1) );
} else {
print( substr( $idx, 3 ) );
}
}
}
}'
}

# The command line consists of the CFLAGS
OCTBSTACK_CFLAGS="$@"

INCLUDE_PATHS=$( find_include_paths )

for ONE_PATH in $INCLUDE_PATHS; do
if test -f "${ONE_PATH}/octypes.h"; then
OCTYPES_H="${ONE_PATH}/octypes.h"
fi
if test -f "${ONE_PATH}/ocpresence.h"; then
OCPRESENCE_H="${ONE_PATH}/ocpresence.h"
fi
if test -f "${ONE_PATH}/ocstackconfig.h"; then
OCSTACKCONFIG_H="${ONE_PATH}/ocstackconfig.h"
fi
if test -f "${ONE_PATH}/ocrandom.h"; then
OCRANDOM_H="${ONE_PATH}/ocrandom.h"
fi
done
16 changes: 8 additions & 8 deletions dist.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ while [[ $# -gt 0 ]]; do
echo "--buildonly or -b : Build and exit"
echo "--distonly or -i : Build distributable tree and exit"
echo "--noreinstall or -n : Do not reinstall dev dependencies after distribution"
echo "--reinstallonly or -r : Reinstall dev dependencies and exit"
echo "--reinstallonly or -r : Reinstall regular and dev dependencies and exit"
echo "--help or -h : Print this message and exit"
exit 0
fi
Expand Down Expand Up @@ -114,7 +114,7 @@ if test "x${DO_DIST}x" = "xTRUEx"; then
echo "*** Performing distribution ***"

rm -rf dist &&
mkdir -p dist/${PACKAGE_NAME} &&
mkdir -p "dist/${PACKAGE_NAME}" &&

( if test "x${DISTONLY}x" != "xTRUEx"; then
if ! do_npm_install ${DEBUG}; then
Expand All @@ -126,15 +126,15 @@ if test "x${DO_DIST}x" = "xTRUEx"; then
# https://github.com/npm/npm/issues/5590 is why prune needs to run twice
npm prune --production &&
npm prune --production &&
cp -a AUTHORS.txt index.js lowlevel.js lib MIT-LICENSE.txt node_modules README.md dist/${PACKAGE_NAME} &&
mkdir -p dist/${PACKAGE_NAME}/build/${MODULE_LOCATION} &&
cp build/${MODULE_LOCATION}/iotivity.node dist/${PACKAGE_NAME}/build/${MODULE_LOCATION} &&
cp -a AUTHORS.txt index.js lowlevel.js lib MIT-LICENSE.txt node_modules README.md "dist/${PACKAGE_NAME}" &&
mkdir -p "dist/${PACKAGE_NAME}/build/${MODULE_LOCATION}" &&
cp "build/${MODULE_LOCATION}/iotivity.node" "dist/${PACKAGE_NAME}/build/${MODULE_LOCATION}" &&
if test -d deps; then
mkdir -p dist/${PACKAGE_NAME}/deps/iotivity/lib
cp deps/iotivity/lib/liboctbstack.so dist/${PACKAGE_NAME}/deps/iotivity/lib
mkdir -p "dist/${PACKAGE_NAME}/deps/iotivity/lib"
cp "deps/iotivity/lib/liboctbstack.so dist/${PACKAGE_NAME}/deps/iotivity/lib"
fi
cd dist &&
tar cvjf iotivity.tar.bz2 ${PACKAGE_NAME} &&
tar cvjf iotivity.tar.bz2 "${PACKAGE_NAME}" &&
cd ..
fi

Expand Down
70 changes: 70 additions & 0 deletions generate-constants.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/bin/bash

# Update constants in generated/constants.cc. The command line arguments for this script are used to
# construct the value of the OCTBSTACK_CFLAGS variable, which, in turn, is used for determining
# where the include files are located.
#
# generated/constants.cc.in contains the comment "// The rest of this file is generated". This script
# preserves the file up to and including the comment, and appends generated contents after that. It
# then appends to generated/constants.cc the constant definitions from octypes.h and ocstackconfig.h.
#
# The script also generates the function InitConstants() which the file is expected to export.

. ./constants-and-enums.common.sh

# generated/constants.cc

# Parse header file, extracting constants
parseFileForConstants() { # $1: filename
grep '^#define' < "$1" | \
awk '{
if ( NF > 2 ) {
print( "SET_CONSTANT_" ( ( substr($3, 1, 1) == "\"" ) ? "STRING": "NUMBER" ) " " $2 );
}
}' | \
sort -u | \
awk '{
print( "#ifdef " $2 );
print( " " $1 "(exports, " $2 ");" );
print( "#endif /* def " $2 "*/" );
}'
}

mkdir -p generated

# Copy the boilerplate from the starter file
cat src/constants.cc.in > generated/constants.cc.new || ( rm -f generated/constants.cc.new && exit 1 )

# Add the function header
echo 'void InitConstants(Handle<Object> exports) {' >> generated/constants.cc.new || \
( rm -f generated/constants.cc.new && exit 1 )

# Parse ocstackconfig.h and append to the generated file
echo ' // ocstackconfig.h: Stack configuration' >> generated/constants.cc.new || \
( rm -f generated/constants.cc.new && exit 1 )
parseFileForConstants "${OCSTACKCONFIG_H}" >> generated/constants.cc.new || \
( rm -f generated/constants.cc.new && exit 1 )

# Separate the two sections with a newline
echo '' >> generated/constants.cc.new || ( rm -f generated/constants.cc.new && exit 1 )

# Parse octypes.h and append to the generated file
echo ' // octypes.h: Definitions' >> generated/constants.cc.new || \
( rm -f generated/constants.cc.new && exit 1 )
parseFileForConstants "${OCTYPES_H}" >> generated/constants.cc.new || \
( rm -f generated/constants.cc.new && exit 1 )

# Separate the two sections with a newline
echo '' >> generated/constants.cc.new || ( rm -f generated/constants.cc.new && exit 1 )

# Parse octypes.h and append to the generated file
echo ' // ocrandom.h: Definitions' >> generated/constants.cc.new || \
( rm -f generated/constants.cc.new && exit 1 )
parseFileForConstants "${OCRANDOM_H}" >> generated/constants.cc.new || \
( rm -f generated/constants.cc.new && exit 1 )

# Close the function
echo '}' >> generated/constants.cc.new || ( rm -f generated/constants.cc.new && exit 1 )

# Replace the original file with the generated file
mv -f generated/constants.cc.new generated/constants.cc || ( rm -f generated/constants.cc.new && exit 1 )
57 changes: 57 additions & 0 deletions generate-enums.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash

# Update enums in generated/enums.cc. The command line arguments for this script are used to construct
# the value of the OCTBSTACK_CFLAGS variable, which, in turn, is used for determining where the
# include files are located.
#
# generated/enums.cc contains the comment "// The rest of this file is generated". This script
# preserves the file up to and including the comment, and discards the rest of the file. It then
# appends to generated/enums.cc the enum definitions from octypes.h
#
# The script also generates the function InitEnums() which the file is expected to export.

. ./constants-and-enums.common.sh

# enums.cc

mkdir -p generated

# Copy the boilerplate from the existing file
cat src/enums.cc.in > generated/enums.cc.new || ( rm -f generated/enums.cc.new && exit 1 )

# Parse header for enums
cat "${OCTYPES_H}" "${OCRANDOM_H}" "${OCPRESENCE_H}" | \
grep -v '^$' | \
awk -v PRINT=0 -v OUTPUT="" -v ENUM_LIST="" '{
if ( $0 == "typedef enum" ) PRINT=1;
if ( PRINT == 1 ) {
if ( !( $1 ~ /^[{}]/ ) && $1 != "typedef" ) {
if ( $1 ~ /^[A-Z]/ ) {
OUTPUT = OUTPUT " SET_CONSTANT_NUMBER(returnValue, " $1 ");\n";
} else if ( $1 ~ /^#(if|endif)/ ) {
OUTPUT = OUTPUT $0 "\n";
}
} else if ( $1 ~ /^}/ ) {
ENUM_NAME = $0;
gsub( /^} */, "", ENUM_NAME );
gsub( / *;.*$/, "", ENUM_NAME );
ENUM_LIST = ENUM_LIST " SET_ENUM(exports, " ENUM_NAME ");\n";
print("static Local<Object> bind_" ENUM_NAME "() {\n Local<Object> returnValue = Nan::New<Object>();\n" );
}
else if ( $1 != "typedef" && $1 != "{" ) {
print;
}
if ( $0 ~ /;$/ ) {
PRINT=0;
print( OUTPUT "\n return returnValue;\n}\n" );
OUTPUT="";
}
}
}
END {
print( "void InitEnums(Handle<Object> exports) {\n" ENUM_LIST "}" );
}' | \
sed 's/[,=]);$/);/' >> generated/enums.cc.new || ( rm -f generated/enums.cc.new && exit 1 )

# Replace the original file with the generated file
mv -f generated/enums.cc.new generated/enums.cc || ( rm -f generated/enums.cc.new && exit 1 )
Loading

0 comments on commit 8ae6a5a

Please sign in to comment.