Skip to content

Commit

Permalink
[wip] add command to build individual file
Browse files Browse the repository at this point in the history
  • Loading branch information
biojppm committed May 24, 2020
1 parent 2b4316f commit 3c86a98
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 38 deletions.
15 changes: 11 additions & 4 deletions src/c4/cmany/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,7 @@ def build(self, targets=[]):
self.configure()
self.handle_deps()
if len(targets) == 0:
if self.compiler.is_msvc:
targets = ["ALL_BUILD"]
else:
targets = ["all"]
targets = [self.generator.target_all]
# cmake --build and visual studio won't handle
# multiple targets at once, so loop over them.
for t in targets:
Expand All @@ -285,6 +282,16 @@ def build(self, targets=[]):
# it can come to fail in some corner cases.
self.mark_build_done(cmd)

def build_files(self, target, files):
self.create_dir()
with util.setcwd(self.builddir, silent=False):
self.handle_deps()
try:
cmd = self.generator.cmd([t])
util.runsyscmd(cmd)
except Exception as e:
raise err.CompileFailed(self, cmd, e)

def rebuild(self, targets=[]):
self._check_successful_configure('rebuild')
with util.setcwd(self.builddir, silent=False):
Expand Down
53 changes: 27 additions & 26 deletions src/c4/cmany/generator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from . import cmake
from . import vsinfo
from . import err

from .build_item import BuildItem
from .err import TooManyTargets
Expand Down Expand Up @@ -67,7 +68,12 @@ def configure_args(self, for_json=False, export_compile_commands=True):
pass
return args

def cmd(self, targets, override_build_type=None, override_num_jobs=None):
@property
def target_all(self):
"return the name of the ALL target"
return "ALL_BUILD" if self.is_msvc else "all"

def cmd(self, targets):
if self.is_makefile:
return ['make', '-j', str(self.num_jobs)] + targets
elif self.is_ninja:
Expand All @@ -76,35 +82,30 @@ def cmd(self, targets, override_build_type=None, override_num_jobs=None):
bt = str(self.build.build_type)
if len(targets) > 1:
raise TooManyTargets(self)
if not self.is_msvc:
cmd = ['cmake', '--build', '.', '--target', targets[0], '--config', bt]
else:
# # if a target has a . in the name, it must be substituted for _
# targets_safe = [re.sub(r'\.', r'_', t) for t in targets]
# if len(targets_safe) != 1:
# raise Exception("msbuild can only build one target at a time: was " + str(targets_safe))
# t = targets_safe[0]
# pat = os.path.join(self.build.builddir, t + '*.vcxproj')
# projs = glob.glob(pat)
# if len(projs) == 0:
# msg = "could not find vcx project for this target: {} (glob={}, got={})".format(t, pat, projs)
# raise Exception(msg)
# elif len(projs) > 1:
# msg = "multiple vcx projects for this target: {} (glob={}, got={})".format(t, pat, projs)
# raise Exception(msg)
# proj = projs[0]
# cmd = [self.build.compiler.vs.msbuild, proj,
# '/property:Configuration='+bt,
# '/maxcpucount:' + str(self.num_jobs)]
cmd = ['cmake', '--build', '.', '--target', targets[0], '--config', bt,
'--',
#'/property:Configuration='+bt,
'/maxcpucount:' + str(self.num_jobs)]
cmd = ['cmake', '--build', '.', '--config', bt, '--target', targets[0],
'--parallel', str(self.num_jobs)]
return cmd

def cmd_source_file(self, target, source_file):
"""get a command to build a source file
https://stackoverflow.com/questions/38271387/compile-a-single-file-under-cmake-project
"""
relpath = os.path.relpath(source_file, os.path.abspath(self.build.builddir))
if self.is_makefile:
return ['make', relpath]
elif self.is_ninja:
raise err.NotImplemented()
# https://ninja-build.org/manual.html#_running_ninja
return ['ninja', f'{relpath}^']
else:
bt = str(self.build.build_type)
cmd = ['cmake', '--build', '.', '--target', targets[0], '--config', bt]
return cmd

def install(self):
bt = str(self.build.build_type)
return ['cmake', '--build', '.', '--config', bt, '--target', 'install']
return ['cmake', '--build', '.', '--config', bt, '--target', 'install',
'--parallel', str(self.num_jobs)]

"""
generators: https://cmake.org/cmake/help/v3.7/manual/cmake-generators.7.html
Expand Down
29 changes: 21 additions & 8 deletions src/c4/cmany/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
('configure', ['c']),
('reconfigure', ['rc']),
('build', ['b']),
('build_file', ['bf']),
('rebuild', ['rb']),
('install', ['i']),
('reinstall', ['ri']),
Expand Down Expand Up @@ -169,6 +170,24 @@ def _exec(self, proj, args):
proj.build()


class install(selectcmd):
"""install the selected builds, building before if necessary"""
def _exec(self, proj, args):
proj.install()


class build_file(globcmd):
"""compile the given source files"""
def add_args(self, parser):
super().add_args(parser)
parser.add_argument('target', nargs=1,
help="""the target to which the source files belong""")
parser.add_argument('files', default=[], nargs='*',
help="""the files to compile, relative to the CMakeLists.txt root dir""")
def _exec(self, proj, args):
proj.build_files(args.target, args.files)


class rebuild(globcmd):
"""rebuild the selected builds, selecting by name using a python glob pattern"""
def add_args(self, parser):
Expand All @@ -179,12 +198,6 @@ def _exec(self, proj, args):
proj.rebuild()


class install(selectcmd):
"""install the selected builds, building before if necessary"""
def _exec(self, proj, args):
proj.install()


class reinstall(globcmd):
"""rebuild the selected builds, selecting by name using a python glob pattern"""
def _exec(self, proj, args):
Expand Down Expand Up @@ -236,7 +249,7 @@ def _exec(self, proj, args):

# -----------------------------------------------------------------------------
class create_proj(selectcmd):
"""create cmany.yml alongside CMakeLists.txt to hold project-settings"""
"""[EXPERIMENTAL] create cmany.yml alongside CMakeLists.txt to hold project-settings"""
def add_args(self, parser):
super().add_args(parser)
parser.add_argument('-o', '--output-file', default="cmany.yml",
Expand All @@ -257,7 +270,7 @@ def _exec(self, proj, args):


class export_vs(selectcmd):
"""create CMakeSettings.json, a VisualStudio 2015+ compatible file
"""[EXPERIMENTAL] create CMakeSettings.json, a VisualStudio 2015+ compatible file
outlining the project builds
"""
def _exec(self, proj, args):
Expand Down
5 changes: 5 additions & 0 deletions src/c4/cmany/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ def do_build(build):
build.build(self.targets)
self._execute(do_build, "Build", silent=False, **restrict_to)

def build_files(self, target, files):
def do_build_files(build):
build.build_files(target, files)
self._execute(do_build_files, "BuildFiles", silent=False)

def rebuild(self, **restrict_to):
def do_rebuild(build):
build.rebuild(self.targets)
Expand Down

0 comments on commit 3c86a98

Please sign in to comment.