├── .gitignore ├── .travis.yml ├── Makefile ├── README.md ├── appveyor.yml ├── bin └── myapp.cc ├── binding.gyp ├── common.gypi ├── include └── mylib │ └── interface.h ├── mylib.gyp ├── node-addon └── src │ └── modulename.cpp └── src └── implementation.cc /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | build/ 3 | projects/ 4 | out/ 5 | manual-build 6 | .svn 7 | *pyc 8 | node_modules -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: cpp 2 | 3 | compiler: 4 | - clang 5 | - gcc 6 | 7 | os: 8 | - osx 9 | - linux 10 | 11 | matrix: 12 | exclude: 13 | - os: osx 14 | compiler: gcc 15 | 16 | before_install: 17 | - true 18 | 19 | install: 20 | - true 21 | 22 | before_script: 23 | - make 24 | 25 | script: 26 | - make test 27 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: ./build ./build/out/Release/myapp 2 | 3 | ./deps/gyp: 4 | git clone --depth 1 https://chromium.googlesource.com/external/gyp.git ./deps/gyp 5 | 6 | ./build: ./deps/gyp 7 | deps/gyp/gyp mylib.gyp --depth=. -f make --generator-output=./build 8 | 9 | ./build/out/Release/myapp: ./build/ 10 | make -C ./build V=1 11 | 12 | test: ./build/out/Release/myapp 13 | ./build/out/Release/myapp 14 | 15 | no-gyp-libstdcpp: 16 | mkdir -p out 17 | rm -f out/implementation.o 18 | c++ -Iinclude -O3 -DNDEBUG -c -o out/implementation.o src/implementation.cc 19 | rm -f out/libmylib.dylib 20 | c++ -dynamiclib -o out/libmylib.dylib out/implementation.o 21 | 22 | no-gyp-libcpp: 23 | mkdir -p out 24 | rm -f out/implementation.o 25 | c++ -Iinclude -std=c++11 -stdlib=libc++ -O3 -DNDEBUG -c -o out/implementation.o src/implementation.cc 26 | rm -f out/libmylib.dylib 27 | c++ -dynamiclib -stdlib=libc++ -o out/libmylib.dylib out/implementation.o 28 | 29 | ./node_modules/.bin/node-gyp: 30 | npm install node-gyp 31 | 32 | node: node_modules/.bin/node-gyp 33 | node_modules/.bin/node-gyp configure build 34 | node -e "console.log(require('./build/Release/modulename.node').hello())" 35 | 36 | clean: 37 | rm -rf ./build 38 | rm -rf ./node_modules 39 | rm -rf ./out 40 | rm -rf ./node-addon/build 41 | 42 | 43 | .PHONY: test 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gyp hello world 2 | 3 | [![Build Status](https://secure.travis-ci.org/springmeyer/hello-gyp.svg)](https://travis-ci.org/springmeyer/hello-gyp) 4 | 5 | [![Build status](https://ci.appveyor.com/api/projects/status/9b57lcato29u73uv)](https://ci.appveyor.com/project/springmeyer/hello-gyp) 6 | 7 | [GYP can Generate Your Projects](http://code.google.com/p/gyp/). 8 | 9 | It is a pre-build system that uses a json config to generate optimized 10 | project files for various build tools like make, xcode, visual studio. 11 | 12 | For example, it is able to generate make or scons files that run fast. 13 | 14 | It is also able to generate xcode and visual studio files. 15 | 16 | It's a bit like cmake, except: http://code.google.com/p/gyp/wiki/GypVsCMake 17 | 18 | This demo shows how to build: 19 | 20 | - a hello world c++ library (called 'mylib') 21 | - a command line program that uses/links to it 22 | - a node.js module that depends on mylib via it gyp file 23 | 24 | See also: 25 | 26 | - http://n8.io/converting-a-c-library-to-gyp/ 27 | - http://code.google.com/p/gyp/wiki/GypTesting 28 | 29 | ## Usage 30 | 31 | Just grab this sample project and build it: 32 | 33 | git clone git://github.com/springmeyer/hello-gyp.git 34 | cd hello-gyp 35 | make 36 | 37 | To learn more about how to do this manually, or for different plaforms, read on. 38 | 39 | ## make 40 | 41 | The below commands assume you have gyp installed globally. You can do this like: 42 | 43 | git clone https://chromium.googlesource.com/external/gyp.git 44 | cd gyp 45 | sudo python setup.py install 46 | 47 | ### build 48 | 49 | gyp mylib.gyp --depth=. -f make --generator-output=./build/makefiles 50 | V=1 make -C ./build/makefiles/ 51 | 52 | ### test 53 | 54 | $ ./build/makefiles/out/Release/myapp 55 | hello 56 | 57 | And the node module: 58 | 59 | npm install node-gyp 60 | node_modules/.bin/node-gyp configure build 61 | 62 | Test the node module: 63 | 64 | $ node -e "console.log(require('./build/Release/modulename.node').hello())" 65 | hello 66 | 67 | ## scons 68 | 69 | ### build 70 | 71 | gyp mylib.gyp --depth=. -f scons --generator-output=./build/sconsfiles 72 | scons -C ./build/sconsfiles/ 73 | 74 | ### test 75 | 76 | $ ./build/sconsfiles/Release/myapp 77 | hello 78 | 79 | 80 | ## xcode 81 | 82 | ### build 83 | 84 | gyp mylib.gyp --depth=. -f xcode --generator-output=./build/xcodefiles 85 | xcodebuild -project ./build/xcodefiles/mylib.xcodeproj 86 | 87 | ### test 88 | 89 | # xcode does not respect subdirectory so `build` goes into the main directory 90 | $ ./build/Release/myapp 91 | hello 92 | 93 | ## ninja 94 | 95 | Ninja is a build system designed to be ultra fast. 96 | 97 | Install it like: 98 | 99 | git clone git://github.com/martine/ninja.git 100 | cd ninja 101 | ./bootstrap.py 102 | cp ninja /usr/local/bin/ 103 | 104 | Make sure it works: 105 | 106 | $ ninja 107 | ninja: no work to do. 108 | 109 | Now go back to the hello-gyp project folder and build the sample: 110 | 111 | gyp mylib.gyp --depth=. -f ninja 112 | ninja -v -C out/Release/ -f build.ninja 113 | 114 | ### test 115 | 116 | $ ./out/Release/myapp 117 | hello 118 | 119 | ## Microsft Visual Studio C++ 2010 Expres 120 | 121 | Note, this assumes you've downloaded and installed the free 2010 C++ studio. 122 | 123 | python gyp/gyp mylib.gyp --depth=. -f msvs -G msvs_version=2010 124 | msbuild mylib.sln 125 | 126 | ### test 127 | 128 | c:\hello-gyp>Release\myapp.exe 129 | hello 130 | 131 | 132 | ## Others 133 | 134 | Other formats are some some gyp-specific debugging output 135 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | 2 | platform: 3 | - x64 4 | - x86 5 | 6 | configuration: 7 | - Debug 8 | - Release 9 | 10 | install: 11 | - SET PATH=c:\python27;%PATH% 12 | - SET PATH=C:\Program Files (x86)\MSBuild\12.0\bin\;%PATH% 13 | - git clone --quiet --depth 1 https://chromium.googlesource.com/external/gyp.git gyp 14 | # note windows requires --generator-output to be absolute 15 | - python gyp/gyp_main.py mylib.gyp --depth=. -f msvs -G msvs_version=2013 16 | - set MSBUILD_PLATFORM=%platform% 17 | - if "%MSBUILD_PLATFORM%" == "x86" set MSBUILD_PLATFORM=Win32 18 | - msbuild mylib.sln /clp:Verbosity=minimal /nologo /p:Configuration=%configuration%;Platform=%MSBUILD_PLATFORM% 19 | - .\"%configuration%"\myapp.exe 20 | 21 | build: OFF 22 | 23 | test: OFF 24 | 25 | test_script: OFF 26 | 27 | deploy: OFF 28 | -------------------------------------------------------------------------------- /bin/myapp.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | 6 | int main(int argc, char **argv) { 7 | MyLib::Message msg("hello"); 8 | std::cout << msg.get() << "\n"; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /binding.gyp: -------------------------------------------------------------------------------- 1 | { 2 | "targets": [ 3 | { 4 | "target_name": "modulename", 5 | "sources": [ 6 | "node-addon/src/modulename.cpp" 7 | ], 8 | "dependencies": [ 9 | "mylib.gyp:mylib" 10 | ] 11 | } 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /common.gypi: -------------------------------------------------------------------------------- 1 | { 2 | "conditions": [ 3 | ["OS=='win'", { 4 | "target_defaults": { 5 | "default_configuration": "Release_x64", 6 | "configurations": { 7 | "Debug_Win32": { 8 | "msvs_configuration_platform": "Win32", 9 | }, 10 | "Debug_x64": { 11 | "msvs_configuration_platform": "x64", 12 | }, 13 | "Release_Win32": { 14 | "msvs_configuration_platform": "Win32", 15 | }, 16 | "Release_x64": { 17 | "msvs_configuration_platform": "x64", 18 | } 19 | } 20 | } 21 | }, { 22 | "target_defaults": { 23 | "default_configuration": "Release", 24 | "xcode_settings": { 25 | }, 26 | "configurations": { 27 | "Debug": { 28 | "defines": [ 29 | "DEBUG" 30 | ], 31 | "xcode_settings": { 32 | "GCC_OPTIMIZATION_LEVEL": "0", 33 | "GCC_GENERATE_DEBUGGING_SYMBOLS": "YES" 34 | } 35 | }, 36 | "Release": { 37 | "defines": [ 38 | "NDEBUG" 39 | ], 40 | "xcode_settings": { 41 | "GCC_OPTIMIZATION_LEVEL": "3", 42 | "GCC_GENERATE_DEBUGGING_SYMBOLS": "NO", 43 | "DEAD_CODE_STRIPPING": "YES", 44 | "GCC_INLINES_ARE_PRIVATE_EXTERN": "YES" 45 | } 46 | } 47 | } 48 | } 49 | }] 50 | ] 51 | } 52 | 53 | -------------------------------------------------------------------------------- /include/mylib/interface.h: -------------------------------------------------------------------------------- 1 | #ifndef INCLUDE_MYLIB_INTERFACE_H_ 2 | #define INCLUDE_MYLIB_INTERFACE_H_ 3 | 4 | #include 5 | 6 | namespace MyLib { 7 | 8 | class Message { 9 | public: 10 | explicit Message(std::string const& data); 11 | std::string const& get(); 12 | void set(std::string const& data); 13 | 14 | private: 15 | std::string m_data; 16 | }; 17 | }; 18 | 19 | #endif // INCLUDE_MYLIB_INTERFACE_H_ 20 | -------------------------------------------------------------------------------- /mylib.gyp: -------------------------------------------------------------------------------- 1 | { 2 | "includes": [ 3 | "common.gypi" 4 | ], 5 | "targets": [ 6 | { 7 | "target_name": "mylib", 8 | "product_name": "mylib", 9 | "type": "static_library", 10 | "sources": [ 11 | "src/implementation.cc", 12 | "include/mylib/interface.h" 13 | ], 14 | "include_dirs": [ 15 | "include" 16 | ], 17 | 'direct_dependent_settings': { 18 | 'include_dirs': [ 'include/' ], 19 | } 20 | }, 21 | { 22 | "target_name": "myapp", 23 | "type": "executable", 24 | "sources": [ 25 | "./bin/myapp.cc" 26 | ], 27 | "include_dirs": [ 28 | "include" 29 | ], 30 | "dependencies": [ 31 | "mylib" 32 | ] 33 | } 34 | ] 35 | } 36 | -------------------------------------------------------------------------------- /node-addon/src/modulename.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // C standard library 4 | #include 5 | 6 | using namespace v8; 7 | 8 | Handle Hello(const Arguments& args) { 9 | HandleScope scope; 10 | std::string hellostring("hello"); 11 | return scope.Close(String::New(hellostring.c_str())); 12 | } 13 | 14 | void RegisterModule(Handle target) { 15 | target->Set(String::NewSymbol("hello"), 16 | FunctionTemplate::New(Hello)->GetFunction()); 17 | } 18 | 19 | NODE_MODULE(modulename, RegisterModule); 20 | -------------------------------------------------------------------------------- /src/implementation.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace MyLib { 5 | 6 | Message::Message(std::string const& data) 7 | : m_data(data) {} 8 | 9 | std::string const& Message::get() { 10 | return m_data; 11 | } 12 | 13 | void Message::set(std::string const& data) { 14 | m_data = data; 15 | } 16 | }; 17 | --------------------------------------------------------------------------------