craftr-build / craftr-build-4.x

Frontend for the Craftr build framework.
https://craftr-build.github.io/craftr/
Other
60 stars 14 forks source link

Custom compiler based targets #5

Closed NiklasRosenstein closed 8 years ago

NiklasRosenstein commented 9 years ago

The Module.target() function accepts an optional keyword argument called target_class that can be passed an alternative class to be used instead of craftr.runtime.Target. This could be used as a "plugin hook".

Consider:

from craftr.utils.path import glob, join
C = load_module('compiler')

module.target('Objects',
  target_class = C.ObjectsTarget,
  inputs = glob(join(project_dir, 'source', '*.cpp')),
  base_dir = project_dir,
  build_dir = join(project_dir, 'build', 'obj'),
)

module.target('Program',
  target_class = C.LinkTarget,
  inputs = Objects.outputs,
  output_file = join(project_dir, 'build', 'main'),
)

These custom targets could also implement special support for specific backends, for example Ninja. The custom target class would know how to properly implement auto dependencies for the used compiler in Ninja.

This is just a draft on how such a target subclass could look like:

from craftr.utils.path import move
import craftr
P = load_module('platform')

# ...

class ObjectsTarget(craftr.runtime.Target):

  def __init__(self, module, name, inputs, base_dir, build_dir):
    outputs = move(inputs, base_dir, build_dir, suffix=P.obj)
    command = ['g++', '-c', '%%in', '-o', '%%out', '-MMD', '-MF', '%%out.d']
    super().__init__(module, name, inputs, outputs, command=command)

  def ninja_rule(self):
    return {'deps': '$out.d'}
NiklasRosenstein commented 8 years ago

Irrelevant due to #14.