ccroot.py: add skip_stlib_deps feature

This commit is contained in:
James Harris 2018-12-13 22:30:54 +00:00 committed by ita1024
parent 37a36eabf2
commit 69bac69b5a
13 changed files with 146 additions and 2 deletions

View File

@ -0,0 +1,11 @@
#include <stdio.h>
#include "external_vars.h"
int k = 5;
void print_value_of_k() {
printf("K = %d\n", k);
}

View File

@ -0,0 +1,14 @@
/*
Export internal vars
*/
#ifndef _EXTERNAL_VARS_H
#define _EXTERNAL_VARS_H
extern int k; /* export k */
void print_value_of_k();
extern int r;
#endif /*_EXTERNAL_VARS_H */

View File

@ -0,0 +1,7 @@
# This is a static library that provides a header to include reference to
# internal variables.
bld.stlib(
target='A',
source='external_vars.c',
includes='.',
export_includes='.')

View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include "sum.h"
#include "external_vars.h"
void sum(int j) {
printf("Adding %d...\n", j);
// Add our new value
k += j;
}

View File

@ -0,0 +1,10 @@
/*
Export internal vars
*/
#ifndef _SUM_H
#define _SUM_H
void sum(int j);
#endif /*_SUM_H */

View File

@ -0,0 +1,9 @@
# This script uses libA to do some internal logic. It uses the default
# behavior so it will be re-archived every time libA changes, even if
# changes in libA are only visible at runtime.
bld.stlib(
target='B',
source='sum.c',
use='A',
includes='.',
export_includes='.')

View File

@ -0,0 +1,13 @@
#include <stdio.h>
#include "diff.h"
#include "external_vars.h"
void diff(int j) {
printf("Subtracting %d...\n", j);
// subtract our new value
k -= j;
}

View File

@ -0,0 +1,10 @@
/*
Export internal vars
*/
#ifndef _DIFF_H
#define _DIFF_H
void diff(int j);
#endif /* _DIFF_H */

View File

@ -0,0 +1,10 @@
# This script uses the logic that prevents static libraries from depending on
# eachother. This means that the only way libC is re-archived is if the source
# code file diff.c or any of its depenencies change.
bld.stlib(
target='C',
source='diff.c',
features='skip_stlib_link_deps',
use='A',
includes='.',
export_includes='.')

22
demos/c/stlib-deps/main.c Normal file
View File

@ -0,0 +1,22 @@
#include <stdio.h>
#include "external_vars.h"
#include "sum.h"
#include "diff.h"
int main()
{
/* This should return to whatever the default value is. */
print_value_of_k();
sum(6);
print_value_of_k();
diff(8);
print_value_of_k();
sum(8);
print_value_of_k();
diff(6);
print_value_of_k();
return 0;
}

View File

@ -0,0 +1,11 @@
#! /usr/bin/env python
bld.recurse('libA')
bld.recurse('libB')
bld.recurse('libC')
# Note that main has an implied dependency between our main program and libA
bld.program(
source = 'main.c',
target = 'test_static_link_chain',
use = 'B C')

View File

@ -75,7 +75,7 @@ def configure(conf):
def build(bld):
bld.env.DEFINES=['WAF=1']
bld.recurse('program stlib shlib')
bld.recurse('program stlib stlib-deps shlib')
#bld.install_files('/tmp/foo', 'wscript')
#bld.env.PREFIX='/tmp/foo'
bld.install_files('${PREFIX}/', 'program/a.h program/main.c', relative_trick=False)

View File

@ -238,6 +238,17 @@ def rm_tgt(cls):
setattr(cls, 'run', wrap)
rm_tgt(stlink_task)
@feature('skip_stlib_link_deps')
@before_method('process_use')
def apply_skip_stlib_link_deps(self):
"""
This enables an optimization in the :py:func:wafilb.Tools.ccroot.processes_use: method that skips dependency and
link flag optimizations for targets that generate static libraries (via the :py:class:Tools.ccroot.stlink_task task).
The actual behavior is implemented in :py:func:wafilb.Tools.ccroot.processes_use: method so this feature only tells waf
to enable the new behavior.
"""
self.env.SKIP_STLIB_LINK_DEPS = True
@feature('c', 'cxx', 'd', 'fc', 'asm')
@after_method('process_source')
def apply_link(self):
@ -386,7 +397,11 @@ def process_use(self):
y = self.bld.get_tgen_by_name(x)
var = y.tmp_use_var
if var and link_task:
if var == 'LIB' or y.tmp_use_stlib or x in names:
if self.env.SKIP_STLIB_LINK_DEPS and isinstance(link_task, stlink_task):
# If the skip_stlib_link_deps feature is enabled then we should
# avoid adding lib deps to the stlink_task instance.
pass
elif var == 'LIB' or y.tmp_use_stlib or x in names:
self.env.append_value(var, [y.target[y.target.rfind(os.sep) + 1:]])
self.link_task.dep_nodes.extend(y.link_task.outputs)
tmp_path = y.link_task.outputs[0].parent.path_from(self.get_cwd())