Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Clang on Windows #2295

Open
aminya opened this issue Dec 31, 2020 · 6 comments
Open

Use Clang on Windows #2295

aminya opened this issue Dec 31, 2020 · 6 comments

Comments

@aminya
Copy link

aminya commented Dec 31, 2020

  • Node Version: All
  • Platform: Win 10
  • Compiler: Clang (clang-cli or clang)
  • Module: All

How can we use Clang on Windows to build native modules? I want to use a single open-source compiler for all the platforms. Writing compiler flags for each compiler is not efficient, and they also may have different performance.

This might be related to:

@rvagg
Copy link
Member

rvagg commented Dec 31, 2020

you could just not use node-gyp and do something manual in your package.json "build". node-gyp (& gyp) is designed to make it flexible across platforms but if you just want to enforce clang on your users then make your own build script that does everything and bypass node-gyp entirely.

I'm not sure gyp has full clang for windows support, does it @nodejs/gyp? it'd just be a bunch of hacks on hacks to make that work for an addon wouldn't it?

@gengjiawen
Copy link
Member

For now, it's kind of buggy, not production ready. Maybe after when we resolve nodejs/node#35433.

@TrevorDev
Copy link

@rvagg could you elaborate on "you could just not use node-gyp and do something manual in your package.json"

I can use napi.h in my c++ files and use clang to generate a dll file on windows but I'm confused on what a .node module file is. Is there a way I can convert a dll to one?

@rvagg
Copy link
Member

rvagg commented May 5, 2021

@TrevorDev I can't give you an example off the top of my head, but there are plenty of Node.js addons that don't use node-gyp to build. cmake.js I think is doing their own thing. I've seen plain Makefile builds. The end goal is to build a library that can be dynamically loaded by node and .node is the convention for naming that binary libary. But how you build it is up to you. It just happens that node-gyp has integration in npm, but npm will also run scripts on install so just insert your build process in there. On windows, it's really just a .dll with a different name. I don't know the specifics of the build process but I think you can set an environment variable V to 1 and build someone's addon (via npm install or node-gyp rebuild or whatever) then gyp will show you a verbose compile output and you can see what it's doing. I assume this also works on Windows. You could learn from this process to make your own build script(s) that do what you want. Experiment and you may find a pattern that others can learn from!

@TrevorDev
Copy link

Woot got it working 🚀, thanks for the info.

For anyone else, this command worked for me:
clang++ test.cpp -Inode_modules\node-addon-api\ -IC:\Users\username.cmake-js\node-x64\v10.16.3\include\node -o test.node -shared C:\Users\username.cmake-js\node-x64\v10.16.3\win-x64\node.lib

@aminya
Copy link
Author

aminya commented May 5, 2021

@TrevorDev I'm glad that you made it work.

I have been working on a new project called cmake-ts. It provides a unified interface for cross-compiling prebuilds for other operating systems (including Windows). It fills in the gaps in the Node ecosystem providing an all-in-one solution for building/prebuilding native addons. So, stay tuned for that (it is still unstable).

https://github.com/EmbeddedEnterprises/cmake-ts/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants