Difference between revisions of "Feature Marking and Selection"
(→Checking for Feature Support in Code) |
(→Behind the Scenes) |
||
Line 92: | Line 92: | ||
# A set of configure flags to enable ''minimalist'' mode and for selecting and removing specific features, as mentioned previously. | # A set of configure flags to enable ''minimalist'' mode and for selecting and removing specific features, as mentioned previously. | ||
+ | # When --with-minimalist is turned on, it adds an extra step to the compilation process: ''features'' which performs the rest of the steps below: | ||
# Compile-time ''feature-check'' and ''feature-remove'' scripts that: | # Compile-time ''feature-check'' and ''feature-remove'' scripts that: | ||
#* feature-check: examines a code file for ''netsnmp_feature_XXX()'' lines and creates ''#define'' statements based on what it finds. | #* feature-check: examines a code file for ''netsnmp_feature_XXX()'' lines and creates ''#define'' statements based on what it finds. | ||
#** Requires that the code be examined in reverse order (eg, agent/mibgroup/ before agent/helpers/ and both before snmplib/). | #** Requires that the code be examined in reverse order (eg, agent/mibgroup/ before agent/helpers/ and both before snmplib/). | ||
#* feature-remove: examines the results of the feature-check output and creates .h files specific to individual code sections with ''NETSNMP_FEATURE_REMOVE_XXX'' definitions or ''NETSNMP_FEATURE_HAS_XXX'' definitions. | #* feature-remove: examines the results of the feature-check output and creates .h files specific to individual code sections with ''NETSNMP_FEATURE_REMOVE_XXX'' definitions or ''NETSNMP_FEATURE_HAS_XXX'' definitions. | ||
+ | #** Includes a feature if anything requires or wants it | ||
+ | #** Removes a feature if nothing requires it | ||
+ | #** Removes a feature if something wants it, but --with-out-features requested it to be removed | ||
+ | #** Includes a feature if nothing required or wanted it but --with-features requested it | ||
# A ''Net-SNMP/features.h'' file that "does the right thing". | # A ''Net-SNMP/features.h'' file that "does the right thing". | ||
#* During the run of ''feature-check'' it includes the definitions being produced by the checking | #* During the run of ''feature-check'' it includes the definitions being produced by the checking | ||
#* During compilation time, it includes the various feature.h files that has the ''NETSNMP_FEATURE_REMOVE_XXX'' and ''NETSNMP_FEATURE_HAS_XXX'' tokens. | #* During compilation time, it includes the various feature.h files that has the ''NETSNMP_FEATURE_REMOVE_XXX'' and ''NETSNMP_FEATURE_HAS_XXX'' tokens. | ||
+ | |||
+ | == Notes on the end result == | ||
+ | |||
+ | # '''./configure ; make ; make install''' still works | ||
+ | # '''./configure --with-minimalist ; make ; make install''' works | ||
+ | # For developers working on feature specific code, '''make''' run from the top level directory will continue to update the generated feature header files | ||
+ | #* But... running '''make cleanfeatures''' first is safer if you're heavily modifying provide/require lines |
Revision as of 17:28, 3 January 2011
Note: Features discussed on this page require Net-SNMP version 5.7 or higher.
Contents
Background
Many components of Net-SNMP are present to support certain features in the code. But if the code that requires a feature isn't compiled in, the supporting code still is and needlessly adds to the size of the running executable and libraries.
Design Criteria
- By default, everything must still be included. 3rd-party developers may be making use of code even if internal Net-SNMP code isn't.
- A configure flag (--with-minimialist) to enable minimal code
- --enable-mini-agent should probably turn it on?
- Flags to request including and excluding of features
- --with-features="foo bar"
- --with-out-features="foo bar"
Feature Marking and Requiring Macros
To start with, include <net-snmp/features.h>
#include <net-snmp/features.h>
If you're implementing code that is only needed in certain locations, declare the feature name using the netsnmp_feature_provide() macro:
netsnmp_feature_provide(foo)
If the feature is dependent on the availability of another feature, use the netsnmp_feature_require() or netsnmp_feature_want() macros:
netsnmp_feature_require(foo) netsnmp_feature_want(bar)
In this case, if foo is unavailable a hard-error will be triggered. However, if bar is unavailable the compilation will continue.
Conditionally Requiring and Providing Features
The feature macros are checked in the output of cpp processing, so typical #ifdef macros can be used to conditionally include or provide features.
Providing a feature only if possible
To provide conditional support of a feature depending on the output of configure checks:
#include <net-snmp/net-snmp-config.h> #include <net-snmp/features.h> #ifdef HAVE_FOO_H netsnmp_feature_provide(netsnmpfoo) #endif
Checking whether or not a dependent feature is available
Sometimes you may wish to advertise support for a feature only if a subordinate feature is also available:
#include <net-snmp/net-snmp-config.h> #include <net-snmp/features.h> #ifdef NETSNMP_FEATURE_HAS_BAR netsnmp_feature_provide(foo) #endif /* NETSNMP_FEATURE_HAS_BAR */
Coding Using Features Support
To mark code as removable if a feature is unneeded, surround it with #ifndef markings:
#ifndef NETSNMP_FEATURE_REMOVE_FOO /* normal foo code */ #else /* !NETSNMP_FEATURE_REMOVE_FOO */ char unused_feature_foo; #endif /* !NETSNMP_FEATURE_REMOVE_FOO */
A few important notes:
- Always use the #ifndef version of checking. If the features.h fails to define anything, the feature will be included by default.
- Always include the define name in comments after #else and #endif clauses. Later "code-removal" scripts will depend upon it.
- The char definition is important for avoiding warnings on compilers that complain or refuse to compile empty files.
Checking for Feature Support in Code
If you're writing code that depends on features but isn't "required" (ie, you used netsnmp_feature_want(foo)), then you can test for its support:
#include <net-snmp/features.h> #ifdef NETSNMP_FEATURE_HAS_FOO /* include foo-specific coding here */ #endif /* NETSNMP_FEATURE_HAS_FOO */
Behind the Scenes
Behind the scenes, this support is implemented using a number of techniques:
- A set of configure flags to enable minimalist mode and for selecting and removing specific features, as mentioned previously.
- When --with-minimalist is turned on, it adds an extra step to the compilation process: features which performs the rest of the steps below:
- Compile-time feature-check and feature-remove scripts that:
- feature-check: examines a code file for netsnmp_feature_XXX() lines and creates #define statements based on what it finds.
- Requires that the code be examined in reverse order (eg, agent/mibgroup/ before agent/helpers/ and both before snmplib/).
- feature-remove: examines the results of the feature-check output and creates .h files specific to individual code sections with NETSNMP_FEATURE_REMOVE_XXX definitions or NETSNMP_FEATURE_HAS_XXX definitions.
- Includes a feature if anything requires or wants it
- Removes a feature if nothing requires it
- Removes a feature if something wants it, but --with-out-features requested it to be removed
- Includes a feature if nothing required or wanted it but --with-features requested it
- feature-check: examines a code file for netsnmp_feature_XXX() lines and creates #define statements based on what it finds.
- A Net-SNMP/features.h file that "does the right thing".
- During the run of feature-check it includes the definitions being produced by the checking
- During compilation time, it includes the various feature.h files that has the NETSNMP_FEATURE_REMOVE_XXX and NETSNMP_FEATURE_HAS_XXX tokens.
Notes on the end result
- ./configure ; make ; make install still works
- ./configure --with-minimalist ; make ; make install works
- For developers working on feature specific code, make run from the top level directory will continue to update the generated feature header files
- But... running make cleanfeatures first is safer if you're heavily modifying provide/require lines