···845845 : /* outputs */ : /* inputs */ : /* clobbers */);846846847847848848+ Chapter 20: Conditional Compilation849849+850850+Wherever possible, don't use preprocessor conditionals (#if, #ifdef) in .c851851+files; doing so makes code harder to read and logic harder to follow. Instead,852852+use such conditionals in a header file defining functions for use in those .c853853+files, providing no-op stub versions in the #else case, and then call those854854+functions unconditionally from .c files. The compiler will avoid generating855855+any code for the stub calls, producing identical results, but the logic will856856+remain easy to follow.857857+858858+Prefer to compile out entire functions, rather than portions of functions or859859+portions of expressions. Rather than putting an ifdef in an expression, factor860860+out part or all of the expression into a separate helper function and apply the861861+conditional to that function.862862+863863+If you have a function or variable which may potentially go unused in a864864+particular configuration, and the compiler would warn about its definition865865+going unused, mark the definition as __maybe_unused rather than wrapping it in866866+a preprocessor conditional. (However, if a function or variable *always* goes867867+unused, delete it.)868868+869869+Within code, where possible, use the IS_ENABLED macro to convert a Kconfig870870+symbol into a C boolean expression, and use it in a normal C conditional:871871+872872+ if (IS_ENABLED(CONFIG_SOMETHING)) {873873+ ...874874+ }875875+876876+The compiler will constant-fold the conditional away, and include or exclude877877+the block of code just as with an #ifdef, so this will not add any runtime878878+overhead. However, this approach still allows the C compiler to see the code879879+inside the block, and check it for correctness (syntax, types, symbol880880+references, etc). Thus, you still have to use an #ifdef if the code inside the881881+block references symbols that will not exist if the condition is not met.882882+883883+At the end of any non-trivial #if or #ifdef block (more than a few lines),884884+place a comment after the #endif on the same line, noting the conditional885885+expression used. For instance:886886+887887+#ifdef CONFIG_SOMETHING888888+...889889+#endif /* CONFIG_SOMETHING */890890+848891849892 Appendix I: References850893
···1515=============================================================16161717To build the tests:1818-1918 $ make -C tools/testing/selftests201921202221To run the tests:2323-2422 $ make -C tools/testing/selftests run_tests2323+2424+To build and run the tests with a single command, use:2525+ $ make kselftest25262627- note that some tests will require root privileges.27282828-To run only tests targeted for a single subsystem: (including2929-hotplug targets in limited mode)30293131- $ make -C tools/testing/selftests TARGETS=cpu-hotplug run_tests3030+Running a subset of selftests3131+========================================3232+You can use the "TARGETS" variable on the make command line to specify3333+single test to run, or a list of tests to run.32343333-See the top-level tools/testing/selftests/Makefile for the list of all possible3434-targets.3535+To run only tests targeted for a single subsystem:3636+ $ make -C tools/testing/selftests TARGETS=ptrace run_tests3737+3838+You can specify multiple tests to build and run:3939+ $ make TARGETS="size timers" kselftest4040+4141+See the top-level tools/testing/selftests/Makefile for the list of all4242+possible targets.4343+35443645Running the full range hotplug selftests3746========================================38473939-To build the tests:4040-4848+To build the hotplug tests:4149 $ make -C tools/testing/selftests hotplug42504343-To run the tests:4444-5151+To run the hotplug tests:4552 $ make -C tools/testing/selftests run_hotplug46534754- note that some tests will require root privileges.5555+48564957Contributing new tests5058======================