Skip to content

Commit

Permalink
wip: some more
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Apr 21, 2024
1 parent b2273d9 commit 03ba642
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 97 deletions.
113 changes: 25 additions & 88 deletions bin/cmake-js2.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import fs from 'fs/promises'
import path from 'path'
import { runCommand } from '../rewrite/dist/processHelpers.js'
import { findCmake } from '../rewrite/dist/toolchain.js'
import { BuildSystem } from '../rewrite/dist/buildSystem.js'

const packageJsonStr = await fs.readFile(new URL('../package.json', import.meta.url))
const packageJson = JSON.parse(packageJsonStr.toString())

function parseOptions(args) {
const config = args.config || 'Release'
const sourceDir = (args.directory && path.resolve(args.directory)) || process.cwd()
const buildDir =
const buildDir = path.join(
(args.out && (path.isAbsolute(args.out) ? args.out : path.join(sourceDir, path.resolve(args.out)))) ||
path.join(sourceDir, 'build')
path.join(sourceDir, 'build'),
config,
)

console.log('dirs', sourceDir, buildDir)
return { sourceDir, buildDir }
return { sourceDir, buildDir, config }
}

async function runCmake(args) {
Expand Down Expand Up @@ -60,10 +61,26 @@ async function runBuild(args) {
await buildSystem.ensureConfigured(args._.slice(1))
await buildSystem.build({
target: args.target,
config: args.config || 'Release',
config: options.config,
})
}

async function runClean(args) {
const options = parseOptions(args)
const buildSystem = new BuildSystem(options)

await buildSystem.clean()
}

async function runListGenerators(args) {
const options = parseOptions(args)
const buildSystem = new BuildSystem(options)
const generators = await buildSystem.getGenerators()

console.log('Available generators:')
console.log(generators.map((g) => ' - ' + g).join('\n'))
}

const args = await yargs(hideBin(process.argv))
// .parserConfiguration({ // TODO - this would be nice to have
// 'unknown-options-as-args': true,
Expand All @@ -74,10 +91,11 @@ const args = await yargs(hideBin(process.argv))
.command('cmake-path', 'Get the path of the cmake binary used', {}, runCmakePath)
.command('configure', 'Configure CMake project', {}, runConfigure)
.command('build', 'Build the project (will configure first if required)', {}, runBuild)
// .command('clean', 'Clean the project directory')
.command('clean', 'Clean the project directory', {}, runClean)
// .command('reconfigure', 'Clean the project directory then configure the project')
// .command('rebuild', 'Clean the project directory then build the project')
// .command('compile', 'Build the project, and if build fails, try a full rebuild')
.command('list-generators', 'List available generators', {}, runListGenerators)
.demandCommand()
.options({
// l: {
Expand Down Expand Up @@ -162,35 +180,12 @@ const args = await yargs(hideBin(process.argv))
// describe: 'use the specified C++ compiler',
// type: 'string',
// },
// r: {
// alias: 'runtime',
// demand: false,
// describe: 'the runtime to use',
// type: 'string',
// },
// v: {
// alias: 'runtime-version',
// demand: false,
// describe: 'the runtime version to use',
// type: 'string',
// },
// a: {
// alias: 'arch',
// demand: false,
// describe: 'the architecture to build in',
// type: 'string',
// },
// p: {
// alias: 'parallel',
// demand: false,
// describe: 'the number of threads cmake can use',
// type: 'number',
// },
// CD: {
// demand: false,
// describe: 'Custom argument passed to CMake in format: -D<your-arg-here>',
// type: 'string',
// },
// i: {
// alias: 'silent',
// describe: 'Prevents CMake.js to print to the stdio',
Expand Down Expand Up @@ -255,19 +250,6 @@ const args = await yargs(hideBin(process.argv))

// log.verbose('CON', 'Parsing arguments')

// // Extract custom cMake options
// const customOptions = {}
// for (const arg of process.argv) {
// if (arg.startsWith('--CD')) {
// const separator = arg.indexOf('=')
// if (separator < 5) continue
// const key = arg.substring(4, separator)
// const value = arg.substring(separator + 1)
// if (!value) continue
// customOptions[key] = value
// }
// }

// const options = {
// directory: argv.directory || null,
// debug: argv.debug,
Expand All @@ -282,49 +264,12 @@ const args = await yargs(hideBin(process.argv))
// preferClang: argv.C,
// cCompilerPath: argv.cc,
// cppCompilerPath: argv.cxx,
// runtime: argv.r,
// runtimeVersion: argv.v,
// arch: argv.a,
// cMakeOptions: customOptions,
// silent: argv.i,
// out: argv.O,
// config: argv.B,
// parallel: argv.p,
// extraCMakeArgs: argv._.slice(1),
// }

// log.verbose('CON', 'options:')
// log.verbose('CON', util.inspect(options))

// const command = argv._[0] || 'build'

// log.verbose('CON', 'Running command: ' + command)

// const buildSystem = new BuildSystem(options)

// function ifCommand(c, f) {
// if (c === command) {
// f()
// return true
// }
// return false
// }

// function exitOnError(promise) {
// promise.catch(function () {
// process.exit(1)
// })
// }

// function install() {
// exitOnError(buildSystem.install())
// }
// function configure() {
// exitOnError(buildSystem.configure())
// }
// function build() {
// exitOnError(buildSystem.build())
// }
// function clean() {
// exitOnError(buildSystem.clean())
// }
Expand All @@ -337,11 +282,3 @@ const args = await yargs(hideBin(process.argv))
// function compile() {
// exitOnError(buildSystem.compile())
// }

// let done = ifCommand('install', install)
// done = done || ifCommand('configure', configure)
// done = done || ifCommand('build', build)
// done = done || ifCommand('clean', clean)
// done = done || ifCommand('reconfigure', reconfigure)
// done = done || ifCommand('rebuild', rebuild)
// done = done || ifCommand('compile', compile)
20 changes: 16 additions & 4 deletions rewrite/src/buildSystem.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs/promises'
import path from 'path'
import { runCommand } from './processHelpers'
import { findCmake } from './toolchain'
import { findCmake, getGenerators } from './toolchain'

export interface BuildSystemOptions {
sourceDir: string
Expand Down Expand Up @@ -75,13 +75,12 @@ export class BuildSystem {

const cmakePath = await this.findCmake()

// TODO --parallel should be driven from some environment variable
// TODO --config ??

const buildCommand = [cmakePath, '--build', this.#options.buildDir, '--config', buildOptions.config]
if (buildOptions.target) {
buildCommand.push('--target', buildOptions.target)
}

// TODO --parallel should be driven from some environment variable
// if (this.options.parallel) {
// command.push('--parallel', this.options.parallel)
// }
Expand All @@ -91,4 +90,17 @@ export class BuildSystem {
cwd: this.#options.buildDir,
})
}

async clean(): Promise<void> {
try {
await fs.rm(this.#options.buildDir, { recursive: true })
} catch (e) {
// Ignore
}
}

async getGenerators(): Promise<string[]> {
const cmakePath = await this.findCmake()
return getGenerators(cmakePath, null)
}
}
18 changes: 18 additions & 0 deletions rewrite/src/toolchain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs/promises'
import which from 'which'
import { execFile } from './processHelpers'

export async function findCmake(): Promise<string> {
const overridePath = process.env['CMAKEJS_CMAKE_PATH']
Expand All @@ -21,3 +22,20 @@ export async function findCmake(): Promise<string> {
return res
}
}

export async function getGenerators(cmakePath: string, log: any): Promise<string[]> {
const generators: string[] = []

// parsing machine-readable capabilities (available since CMake 3.7)
try {
const stdout = await execFile([cmakePath, '-E', 'capabilities'])
const capabilities = JSON.parse(stdout)
return capabilities.generators.map((x: any) => x.name)
} catch (error) {
if (log) {
log.verbose('TOOL', 'Failed to query CMake capabilities (CMake is probably older than 3.7)')
}
}

return generators
}
10 changes: 5 additions & 5 deletions share/cmake/CMakeJS.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,10 @@ endif()

if (APPLE)
# TODO: Does macos need the following still?
target_compile_options(cmake-js PRIVATE "-D_DARWIN_USE_64_BIT_INODE=1")
target_compile_options(cmake-js PRIVATE "-D_LARGEFILE_SOURCE")
target_compile_options(cmake-js PRIVATE "-D_FILE_OFFSET_BITS=64")
target_link_options(cmake-js PRIVATE "-undefined dynamic_lookup")
target_compile_options(cmake-js PUBLIC "-D_DARWIN_USE_64_BIT_INODE=1")
target_compile_options(cmake-js PUBLIC "-D_LARGEFILE_SOURCE")
target_compile_options(cmake-js PUBLIC "-D_FILE_OFFSET_BITS=64")
target_link_options(cmake-js PUBLIC "-undefined dynamic_lookup")
endif()

function(_cmakejs_export_target name)
Expand Down Expand Up @@ -569,7 +569,7 @@ function(cmakejs_create_node_api_addon name)
SUFFIX ".node"

ARCHIVE_OUTPUT_DIRECTORY "${CMAKEJS_BINARY_DIR}/lib" # Actually we might not need to enforce an opinion here!
LIBRARY_OUTPUT_DIRECTORY "${CMAKEJS_BINARY_DIR}/lib" # Instead, we call 'cmakejs_create_addon_bindings()'
LIBRARY_OUTPUT_DIRECTORY "${CMAKEJS_BINARY_DIR}" # Instead, we call 'cmakejs_create_addon_bindings()'
RUNTIME_OUTPUT_DIRECTORY "${CMAKEJS_BINARY_DIR}/bin" # on this target, and the user can just 'require()' that file!

# # Conventional C++-style debug settings might be useful to have...
Expand Down

0 comments on commit 03ba642

Please sign in to comment.