JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.2p0 | CONFIDENTIAL | CodeSecure Inc |
This page describes how to extend your CodeSonar-facing build so that your CodeSonar project includes:
If your Kotlin project targets the JVM you can also analyze it with cs-java-scan, which will produce warnings from a subset of the Java warning classes.
This page describes how to extend your CodeSonar-facing build so that your CodeSonar project includes:
Incorporating Kotlin components follows the standard process for including a tier 3 component. The specializations to that process are summarized in the following table. For detailed instructions, see Including Kotlin Components In Your CodeSonar project, below. We also provide an example Makefile.
| Language Tools | You do not need a Kotlin compiler in order to run detekt or add source files to your CodeSonar project. |
|---|---|
| Third-Party Analyzer | Kotlin detekt |
| Third-Party Analysis Step | You do not need to explicitly invoke detekt: it is invoked in the codesonar kotlin_scan.py subcommand. |
| Extend CodeSonar-facing Build | Invoke codesonar kotlin_scan.py on your Kotlin source files (.kt, .kts). This will analyze the files with detekt, then add the detekt SARIF results and the source files to the CodeSonar project. |
Incorporating Kotlin components follows the standard process for including a tier 3 component.
Do you already have a detekt installation?
Note that detekt requires a
JVM in order to run.
If you do not have a JVM, you can generally use the one shipped
with CodeSonar.
If you have not already set up a CodeSonar-facing build for your regular software project, you will need to create one.
The following diagram illustrates the extended software build described above, and its interactions with the CodeSonar build/analysis.
In this section (and throughout this manual), $CSONAR indicates the CodeSonar installation directory.
When a SARIF file is imported into a CodeSonar project, the SARIF importer determines an associated warning class for each rule object in the SARIF rules, creating this warning class if it does not already exist.
The SARIF importer has special handling for warning classes derived from SARIF produced by detekt. For more information, see Warning Classes Corresponding to detekt Rules.
To analyze Kotlin source files with detekt and then add the source files and analysis results to a project, CodeSonar observes the execution of a command that incorporates one or more invocations of codesonar kotlin_scan.py.
where:
| [file_or_dir ...] |
is a space-separated list of source file or directory paths.
The set of files specified with file_or_dir arguments can be modified with -include-sources and -exclude-sources. Note: the file_or_dir arguments are not glob patterns (as used for -include-sources and -exclude-sources). However, your shell may expand patterns into file and directory names. If there are no file_or_dir arguments and no -include-sources arguments, the codesonar kotlin_scan.py subcommand will fail. |
|---|---|
| [-C rundir] [-directory rundir] |
detekt will be run in rundir. If -C/-directory is not specified,
detekt will be run in the current working directory.
Any file_or_dir, -include-sources file_pat, and -exclude-sources file_pat arguments are interpreted relative to rundir. For example:
|
| [-include-sources
file_pat] [-exclude-sources file_pat] |
All Kotlin source files (.kt,
.kts) indicated by the
-C/-directory and
file_or_dir argument
combination will be analyzed with detekt. By default, this set of Kotlin source files is then imported into the CodeSonar project. Use -include-sources and -exclude-sources to adjust the imported set by adding or removing files. file_pat is a recursive glob pattern representing a source file path. It matches all files whose file path matches file_pat. Relative paths are interpreted with respect to the directory specified with -C/-directory. When a single codesonar kotlin_scan.py invocation contains a combination of -include-sources and -exclude-sources options, they are applied in the order in which they appear on the command line. Note:
|
| [-detekt-cli detekt_path] | Specifies the path to the detekt installation to use for
analysis. If -detekt-cli detekt_path is not specified, the detekt installation shipped with CodeSonar is used. |
| [-use-external-jdk] |
Specifies that codesonar
kotlin_scan.py should invoke detekt using the JDK from
the local environment.
If -use-external-jdk is specified, codesonar kotlin_scan.py must be invoked in an environment where:
If -use-external-jdk is not specified, codesonar kotlin_scan.py will try to use the JDK shipped with CodeSonar. |
| [-X+detekt_options_list] |
The +-separated
list of options and values in detekt_options_list are
passed to the detekt invocation.
You can specify multiple detekt options in a single
detekt_options_list; you can also specify -X multiple times in a single codesonar kotlin_scan.py
invocation.
codesonar kotlin_scan.py
-X+--report+xml:detekt_report.xml+--jvm-target+9 [...
remaining kotlin_scan.py options]
codesonar kotlin_scan.py -X+--report+xml:detekt_report.xml
-X+--jvm-target+9 [... remaining kotlin_scan.py
options]
Do not use -X to pass detekt
options that specify source file inputs. Specify this
information directly in the codesonar kotlin_scan.py command
with direct file_or_dir arguments, -include-sources, and -exclude-sources.
|
| [-sarif-output sarif_fname] |
Specifies that the SARIF output from detekt should be written
to file sarif_fname. The primary use case for this option is in projects for which you wish to retain the SARIF results after the CodeSonar build/analysis has finished If -sarif_output sarif_fname is not specified, codesonar kotlin_scan.py will use a temporary file for the SARIF output. |
| [-source-max-bytes num] | Specifies a maximum size of num bytes for imported
Kotlin source files: files larger than this maximum size will be
ignored by the importer. If -source-max-bytes is not specified, the importer will ignore files larger than 500KB. |
| [@infile] |
infile is a text file
containing a list of arguments to be added to the codesonar kotlin_scan.py command
line.
|
The codesonar kotlin_scan.py subcommand imports Kotlin source files into the CodeSonar project using the same mechanism as codesonar import_sarif.py, because CodeSonar does not have a full front end for Kotlin.
There are several consequences when files are directly imported with codesonar import_sarif.py.
An example Makefile for a Kotlin project is shown below. We also provide an example Makefile for a mixed-language project that has a Kotlin component: see Including Tier 3 Components in a CodeSonar Project: Example Makefile
.PHONY: all clean csonar_facing all: kotlin_component.jar # For the sake of this example, target the JVM. The steps required to # create a CodeSonar-facing build for Kotlin are the same regardless of # the target platform. kotlin_component.jar: $(SRCDIR)/kotlin_component.kt kotlinc "$(SRCDIR)/kotlin_component.kt" -d kotlin_component.jar # Invoke codesonar kotlin_scan.py to analyze the Kotlin source code with detekt, # then import the SARIF results and the source file into the project. csonar_facing: $(SRCDIR)/kotlin_component.kt "$(CSONAR)"/codesonar/bin/codesonar kotlin_scan.py \ "$(SRCDIR)/kotlin_component.kt" clean: rm *.jar
We have provided a minimal sample project so you can experiment with the example Makefile.
SRCDIR=.
SRCDIR=.
CSONAR=path/to/your/codesonar/installation
# on Windows this will be CSONAR=C:\\Program Files\\CodeSecure\\CodeSonar or similar
If you are interested, you can try building other targets in the example Makefile.
To report problems with this documentation, please visit https://support.codesecure.com/.